Python第19天 memcached启动脚本v2

文章目录

2018-12-7 15:14:11

3.6-3.8 rc脚本

  • memcached启动脚本v2
#!/usr/bin/python
#conding:utf8
import sys
from subprocess import Popen,PIPE
import os

class Process(object):
    '''memcached rc scrip'''
    args = {'USER':'memcached',
            'PORT':11211,
            'MAXCONN':1024,
            'CACHESIZE':64,
            'OPTIONS':''}
    def __init__(self,name,program,workdir):
        self.name = name
        self.program = program
        self.workdir = workdir
    def _init(self):
        '''/var/tmp/memcached  pid'''
        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',self.name],stdout=PIPE)
        self.pid = p.stdout.read().strip()
        return self.pid

    def _readConf(self,f):

        with open(f,'r') as f:
            line = f.readlines()
            return dict([i.strip().replace('"','').split('=') for i in line])

    def _passArgs(self):
        conf = self._readConf('/etc/sysconfig/memcached')
        if 'USER' in conf:
            self.args['USER'] = conf['USER']
        if 'PORT' in conf:
            self.args['PORT'] = conf['PORT']
        if 'MAXCONN' in conf:
            self.args['MAXCONN'] = conf['MAXCONN']
        if 'CACHESIZE' in conf:
            self.args['CACHESIZE'] = conf['CACHESIZE']
        options = ['-u',self.args['USER'],
                   '-p',self.args['PORT'],
                   '-m',self.args['CACHESIZE'],
                   '-c',self.args['MAXCONN']]
        return options
    def start(self):
        self._init()
        if not self._getPid():
            cmd = [self.program] + self._passArgs() + ['-d','-P',self._pidFile()]
            #print cmd
            p = Popen(cmd, stdout=PIPE)
            #self.pid = p.pid
            #self._writePid()
            print '/033[1;32m%s start Sucessful/033[0m' %self.name
        else:
            print '/033[1;32m%s is running.../033[0m' %self.name

    def stop(self):
        pid = self._getPid()
        if pid:
            os.kill(int(pid),15)
            if os.path.exists(self._pidFile()):
                os.remove(self._pidFile())
                print '/033[1;32m%s is stoped/033[0m' %self.name
        else:
            print '/033[1;32m%s is not running... /033[0m' %self.name
    def restart(self):
        self.stop()
        self.start()
    def status(self):
        pid = self._getPid()
        if pid:
            print '/033[1;32m%s is running... /033[0m' %self.name
        else:
            print '/033[1;32m%s is not running... /033[0m' %self.name

    def help(self):
        print "/033[1;33mUsage start|stop|status|restart /033[0m "

def main():
    name = 'memcached'
    prog = '/usr/bin/memcached'
    wd = '/var/tmp/memcached'
    pm = Process(name = name,
                 program = prog,
                 workdir = wd)
    try:
        cmd = sys.argv[1]
    except IndexError, e:
        pm.help()
        sys.exit()
    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()

给TA打赏
共{{data.count}}人
人已打赏
开发

Python第47天 权限数据需求分析

2023-9-11 18:33:05

开发

Python第十天 函数定义及参数

2023-9-11 18:33:07

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索