01月12日 四周五次Python基础进阶
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了01月12日 四周五次Python基础进阶相关的知识,希望对你有一定的参考价值。
3.5 rc脚本(类的定义与脚本的结构)
3.6 rc脚本(start方法)
3.7 rc脚本(stop和status方法)
3.8 rc脚本(以daemon方式启动)
3.5 rc脚本(类的定义与脚本的结构)/3.6 rc脚本(start方法)/3.7 rc脚本(stop和status方法)
import sys
import os
from subprocess import Popen, PIPE
class Process(object):
‘‘‘memcached rc script‘‘‘
def __init__(self, name, program, args, 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 _writhPid(self):
if self.pid:
with open(self._pidFile(), ‘w‘) as fd:
fd.write(str(self.pid))
def start(self):
pid = self._getPid()
if pid:
print ("%s is running..." % self.name)
sys.exit()
self._init()
cmd = self.program + ‘ ‘ + self.args
p = Popen(cmd, stdout = PIPE, shell = True)
self.pid = p.pid
self._writhPid()
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: {start | stop | startus | restart}" % __file__)
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 as 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()
3.8 rc脚本(以daemon方式启动)
import sys
import os
from subprocess import Popen, PIPE
class Process(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.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 _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 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‘]
if ‘OPTIONS‘ in conf:
self.args[‘OPTIONS‘] = conf[‘OPTIONS‘]
options = [‘-u‘, self.args[‘USER‘],
‘-p‘, self.args[‘PORT‘],
‘-c‘, self.args[‘MAXCONN‘],
‘-m‘, self.args[‘CACHESIZE‘]]
os.system("chown %s %s" % (self.args[‘USER‘], self.workdir))
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._writhPid()
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: {start | stop | startus | restart}" % __file__)
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, workdir = wd)
try:
cmd = sys.argv[1]
except IndexError as 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()
以上是关于01月12日 四周五次Python基础进阶的主要内容,如果未能解决你的问题,请参考以下文章