博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day33-python阶段性复习七
阅读量:6541 次
发布时间:2019-06-24

本文共 2537 字,大约阅读时间需要 8 分钟。

 
rc脚本练习
#!/usr/bin/env python#coding:utf8import sysimport osfrom subprocess import Popen, PIPEclass Process(object):'''memcached rc script'''args = {
'USER':'memcached','PORT':11211,'MAXCONN':1024,'CACHESIZE':'64','OPTIONS':''}def __init__(self, name, program , workdir):self.name = nameself.program = program17 self.workdir = workdirdef _init(self):'''/var/tmp/memcached'''if not os.path.exists(self.workdir):os.mkdir(self.workdir)os.chdir(self.workdir)def _pidFile(self):'''/var/tpm/memcached/memcached.pid'''return os.path.join(self.workdir,"%s.pid" % self.name)27 def _writhPid(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 linesdef _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 'CACHESEIZE' 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))return options52 def start(self):pid = self._getPid()if pid:print "%s is running..." % self.namesys.exit()self._init()59 cmd = [self.program] + self._parseArgs() + ['-d','-P', self._pidFileprint cmdp = Popen(cmd, stdout=PIPE)# self.pid = p.pid# self._writhPid()print "%s start Sucessful" % self.name66 def _getPid(self):p =Popen(['pidof', self.name], stdout=PIPE)pid = p.stdout.read().strip()return piddef 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.name78 def restart(self):self.stop()self.start()82def status(self):pid = self._getPid()if pid:print "%s is already runing" % self.nameelse:print "%s is not runing" % self.name90 def help(self):print "Usage: %s {start|stop|status|restart} " % __file__93 def main():name = 'memcached'prog = '/usr/bin/memcached'args = '-u nobody -p 11211 -c 1024 -m 64'wd = '/var/tmp/memcached'pm = Process(name=name,program=prog,# args=args,workdir=wd)try:cmd = sys.argv[1]except IndexError,e:print "Option error"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()

 

转载于:https://www.cnblogs.com/wxp997/p/7979142.html

你可能感兴趣的文章
第一次软件工程作业
查看>>
Chapter 2 C#语句---选择语句
查看>>
centos7.5 下编译安装PHP7.2
查看>>
CF1096E The Top Scorer
查看>>
PNG透明兼容IE6的几种方法(转)
查看>>
Python爬虫(一)
查看>>
Eclipse中tomcat启动时提示java.lang.ClassNotFoundException: XXX class
查看>>
10项可用性结论与指南
查看>>
linux EXT4格式分区扩容
查看>>
Theano:LSTM源码解析
查看>>
MyPython-->进阶篇-->测试代码
查看>>
Docker容器安装
查看>>
attr和prop的区别 chosen插件
查看>>
Linux入门学习教程:虚拟机体验之KVM篇
查看>>
天池大数据周冠军分享|附移动推荐算法赛答辩会Top5选手PPT
查看>>
HDU 2870 Largest Submatrix
查看>>
HTML5 图片缩放功能
查看>>
VirtualBox 4.2 released !
查看>>
Windows线程同步API
查看>>
内存调试技巧
查看>>