说明:使用类的方式编写程序启动脚本(memcached)

 import time
import os
from subprocess import Popen,PIPE class Process(object):
'mamcached rc scripts'
def __init__(self,name,program,args,workdir):
self.name = name
self.program = program
self.args = args
self.workdir = workdir
self._init() def _init(self):
'''/var/tmp/memcached'''
if not os.path.exists(self.workdir):
os.mkdir(self.workdir)
os.chdir(self.workdir) def _pidfile(self):
'''/var/tmp/memcached/memcached.pid'''
return os.path.join(self.workdir,'%s.pid' % self.name) def _writepid(self):
if self.pid:
with open(self._pidfile(),'w') as fd:
fd.write(str(self.pid)) def _getpid(self):
p = Popen(['pidof','memcached'],stdout=PIPE)
pid = p.stdout.read().strip('\n')
if pid:
return int(pid)
return None def start(self):
cmd = self.program + ' ' + self.args
p = Popen(cmd,stdout=PIPE,shell=True)
self.pid = p.pid
self._writepid()
print('%s start Successful ' % self.name) def stop(self):
result = self._getpid()
if result:
os.kill(result,15)
if os.path.exists(self._pidfile()):
os.remove(self._pidfile())
print('The %s are stoped !' % self.name) def restart(self):
self.stop()
time.sleep(1)
self.start() def status(self):
pid = self._getpid()
if pid:
print('The %s is running , The pid is %s' % (self.name,pid))
else:
print('The %s is not running!' % self.name) def help(self):
print('Usage: %s {start|restart|status|stop}' % __file__ ) def main():
name = 'memcached'
program = '/usr/bin/memcached'
args = '-u nobody -p 11211 -c 1024 -m 64 '
wd = '/var/tmp/memcached'
pm = Process(name,program,args,wd)
try:
cmd = sys.argv[1]
except IndexError as e:
sys.exit('Option Error')
if cmd == 'start':
pm.start()
elif cmd == 'stop':
pm.stop()
elif cmd == 'restart':
pm.restart()
elif cmd == 'status':
pm.status()
else:
pm.help() if __name__ == '__main__':
main()

memcached启动脚本(class练习)的更多相关文章

  1. memcached启动脚本以及telnet测试

    memcached -m 1024 -u root -p 11211 -c 1024 -P /var/memcached.pid -d memcached 1.2.2 -p <num> T ...

  2. Memcached启动脚本

    ched: MemCached Daemon # # chkconfig: - # description: MemCached Daemon # # Source function library. ...

  3. 实例:用类来写一个 memcached 启动脚本

    [root@localhost ~]$ yum install -y memcached #!/usr/bin/env python #-*- coding:utf-8 -*- import os i ...

  4. 自己编写服务启动脚本(一):functions文件详细分析和说明

    本文目录: 1.几个显示函数2.action函数3.is_true和is_false函数4.confirm函数5.pid检测相关函数 5.1 checkpid.__pids_var_run和__pid ...

  5. logstash服务启动脚本

    logstash服务启动脚本 最近在弄ELK,发现logstash没有sysv类型的服务启动脚本,于是按照网上一个老外提供的模板自己进行修改 #添加用户 useradd logstash -M -s ...

  6. 改进uwsgi启动脚本,使其支持多个独立配置文件

    最近在研究flask,在架设运行环境的时候犯了难.因为我想把每个独立的应用像NGINX处理多个网站那样,每个应用单独一个配置文件.而网上流传的uwsgi启动脚本都只支持单个配置文件.虽然有文章说可以把 ...

  7. linux nginx 启动脚本

    linux nginx 启动脚本 [root@webtest76 ~]# vi /etc/init.d/nginx #!/bin/bash # nginx Startup script for the ...

  8. busybox rootfs 启动脚本分析(二)

    上次分析了busybox的启动脚本,这次分析一下init.d中一些脚本的内容. 参考链接 http://www.cnblogs.com/helloworldtoyou/p/6169678.html h ...

  9. Tomcat启动脚本

    记录一个比较好的tomcat启动脚本,截取<OneinStack>,修改如下两个参数即可用. 使用之前修改下面2个参数: #Location of JAVA_HOME (bin files ...

随机推荐

  1. 一个关于sql更新的小笔记

    一直在sqlserver下写东西,突然用mysql有些语法发生了改变,有点折腾 (MS SQL Server)语句:update A set  a.Name =   b.Name   from   A ...

  2. ProxySQL读写分离测试(续)

      Preface       I've implemented ProxySQL on PXC yesterday but got some errors when configured query ...

  3. 「日常训练」「小专题·USACO」 Broken Necklace(1-2)

    题意 圆形链条,打断一处可以形成一条链.问在哪个地方开始打断,能够形成最大的连续颜色(白色视作同样的颜色)? 分析 说起来很高级,但是我们实际上并不需要穷举打断的地方,只需要把串重复三回啊三回.然后从 ...

  4. Selenium Grid 环境搭建 碰到的unable to access server

    1. Slenenium Grid的环境部署, 前提条件: JDK,JRE都已经安装, selenium的standalone jar包放在磁盘 执行如下命令,报错: 2. 在cmd窗口里切换到jar ...

  5. tensorflow的几种优化器

    最近自己用CNN跑了下MINIST,准确率很低(迭代过程中),跑了几个epoch,我就直接stop了,感觉哪有问题,随即排查了下,同时查阅了网上其他人的blog,并没有发现什么问题 之后copy了一篇 ...

  6. 九度OJ--Q1168

    import java.util.Scanner; public class q1168 { public static void main(String[] args) { Scanner scan ...

  7. linux备忘录-程序管理与SELinux

    知识点 程序与程序(Process and Program) 通过执行一条指令或程序,则可以触发一个事件,并获得一个PID.当我们需要启动一个程序时,我们是启动一个二进制文件(binary file) ...

  8. 图书 Framework 设计指南: 可重用 .NET 库的约定、惯用法和模式 引出资料

    文章:框架设计准则     --微软 地址:https://docs.microsoft.com/zh-cn/dotnet/standard/design-guidelines/index

  9. DataGridView使用

    DataGridView控件概述 DataGridView 控件代码目录(Windows 窗体) 未绑定数据列 定义:可能想要显示并非来自数据源的一列数据,这种列称为未绑定列. 数据格式示例 如何:设 ...

  10. 多线程 定时器 Timer TimerTask

    定时器是一种特殊的多线程,使用Timer来安排一次或者重复执行某个任务 package org.zln.thread; import java.util.Date; import java.util. ...