#!/usr/bin/python#Coding:utf-8import sysimport osfrom subprocess import Popen,PIPEclass Memcached(object): ''' memcached rc script ''' args = {'USER':'memcached','PORT':11211,'MAXCONN':1024,'CACHESIZE':64,'OPTIONS':''} def __init__(self,name,program,workdir): self.name = name self.program = program #self.args = args self.workdir = workdir 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 _readConf(self,f): with open(f) as fd: lines = fd.readlines() return dict([i.strip().replace('"','').split('=') for i in lines]) def _parseArgs(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']] os.system('chown %s %s' % (self.args['USER'],self.workdir)) ''' 这个地方要修改工作目录的权限,用户为memcached,需要有写目录的权限。 ''' return options def start(self): pID = self._getPID() if pID: print '%s is running...' % self.name sys.exit() self._init() cmd = [self.program] + self._parseArgs() + ['-d','-P',self._pIDfile()] print cmd p = Popen(cmd,stdout = PIPE) #self.pID = p.pID #self._writePID() # 如果程序选项中有-P选项,那么应用程序会自动去写pID文件,不用手动去写,而且要注意pID与手动写入的pID有何区别。 print '%s start Sucessful' % self.name def _getPID(self): p = Popen(['pIDof',self.name],stdout=PIPE) pID = p.stdout.read().strip() return pID def stop(self): pID = self._getPID() if pID: os.kill(int(pID),15) if os.path.exists(self._pIDfile()): os.remove(self._pIDfile()) print '%s is stopped' % self.name def restart(self): self.stop() self.start() def status(self): pID = self._getPID() if pID: print '%s is already running' % self.name else: print '%s is not running' % self.name def help(self): print 'Usage: %s {start|stop|status|restart}' % __file__def main(): name = 'memcached' prog = '/usr/bin/memcached' #args = '-u nobody -p 11211 -c 1024 -m 64' wdir = '/var/tmp/memcached' rc = Memcached(name,prog,wdir) try: cmd = sys.argv[1] except IndexError,e: print "Option error" sys.exit() if cmd == 'start': rc.start() elif cmd == 'stop': rc.stop() elif cmd == 'restart': rc.restart() elif cmd == 'status': rc.status() else: rc.help()if __name__ == '__main__': main()
总结
以上是内存溢出为你收集整理的使用 python 实现 memcached 的启动服务脚本 rc全部内容,希望文章能够帮你解决使用 python 实现 memcached 的启动服务脚本 rc所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)