一、一般模式
启动选项:start、stop、restart、status
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | #!/usr/bin/env python #encoding:utf-8 import sys import os from subprocess import Popen, PIPE class Process( object ): '''memcached rc script''' def __init__( self ,name,program,argv,workdir): # 初始化类,包含:self、程序名、程序启动文件、执行参数、pid文件路径 self .name = name self .program = program self .argv = argv self .workdir = workdir def __workDir( self ): # 判断pid文件路径,没有则创建 '''/var/tmp/memcached''' if not os.path.exists( "/var/tmp/memcached" ): os.mkdir( "/var/tmp/memcached" ) os.chdir( "/var/tmp/memcached" ) def __pidFile( self ): # 获取pid文件,带路径 '''/var/tmp/memcached/memcached.pid''' return os.path.join( self .workdir, "%s.pid" % self .name) def __writePid( self ): # 将pid写进pid文件中,pid为字符串类型 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 .__workDir() cmd = self .program + " " + self .argv p = Popen(cmd,stdout = PIPE,shell = True ) # 启动进程方式 self .pid = p.pid # 加self,声明为类的属性,便于其他内部函数调用 self .__writePid() print ( "%s start successful" % self .name) def __getPid( self ): p = Popen([ "pidof" , self .name],stdout = PIPE) # 获取pid方式 pid = p.stdout.read().strip() # 过滤pid return pid def stop( self ): pid = self .__getPid() if pid: os.kill( int (pid), 15 ) # 杀掉进程方式,pid为数值类型 if os.path.exists( self .__pidFile()): os.remove( self .__pidFile()) # 删掉pid文件 print ( "%s is stopped" % self .name) def restart( self ): self .stop() self .start() def status( self ): pid = self .__getPid() if pid: print ( "%s is running" % self .name) else : print ( "%s is not running" % self .name) def help ( self ): print ( "Usage: %s {start|stop|restart|status}" % __file__) def main(): name = "memcached" program = "/data/program/memcached/bin/memcached" workdir = "/var/tmp/memcached" argv = "-u nobody -p 11211 -c 1024 -m 64" pm = Process(name = name, program = program, workdir = workdir, argv = argv) # 传参,也可以按顺序只写实参 try : cmd = sys.argv[ 1 ] # 如启动参数有误,则抛出异常,并退出 except Exception as e: raise e 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() |
二、后台模式
说明:后台方式:-d -P XXX.pid ;如有配置文件,则配置文件参数替换掉默认参数
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 | #!/usr/bin/env python #encoding:utf-8 import sys import os from subprocess import Popen, PIPE class Process( object ): '''memcached rc script''' argv = { "USER" : "memcached" , "PORT" : "11211" , "MAXCONN" : "1024" , "CACHESIZE" : "64" , "OPTIONS" : ""} # 默认启动参数 def __init__( self ,name,program,workdir): # 初始化类,包含:self、程序名、程序启动文件、pid文件路径 self .name = name self .program = program self .workdir = workdir def __readConf( self ,f): # 读取配置文件,返回字典 with open (f, "r" ) as fd: lines = fd.readlines() return dict ([i.strip().split( "=" ) for i in lines]) def __parseArgv( self ): # 配置文件替换默认参数 '''/etc/sysconfig/memcached''' conf = self .__readConf( "/etc/sysconfig/memcached" ) if "USER" in conf: self .argv[ "USER" ] = conf[ "USER" ] if "PORT" in conf: self .argv[ "PORT" ] = conf[ "PORT" ] if "MAXCONN" in conf: self .argv[ "MAXCONN" ] = conf[ "MAXCONN" ] if "CACHESIZE" in conf: self .argv[ "CACHESIZE" ] = conf[ "CACHESIZE" ] options = [ "-u" , self .argv[ "USER" ], "-p" , self .argv[ "PORT" ], "-m" , self .argv[ "CACHESIZE" ], "-c" , self .argv[ "MAXCONN" ]] # 替换的参数,以列表形式返回 os.system( "chown %s %s" % ( self .argv[ "USER" ], self .workdir)) # 修改pid文件目录的属主为启动用户,才能写pid文件 print ( "启动参数为:%s" % str (options)) return options def __workDir( self ): # 判断pid文件路径,没有则创建 '''/var/tmp/memcached''' if not os.path.exists( "/var/tmp/memcached" ): os.mkdir( "/var/tmp/memcached" ) os.chdir( "/var/tmp/memcached" ) def __pidFile( self ): # 获取pid文件,带路径 '''/var/tmp/memcached/memcached.pid''' return os.path.join( self .workdir, "%s.pid" % self .name) def start( self ): pid = self .__getPid() if pid: print ( "%s is running" % self .name) sys.exit() self .__workDir() cmd = [ self .program] + self .__parseArgv() + [ "-d" , "-P" , self .__pidFile()] # 启动参数,后台启动方式,按指定文件自动写pid p = Popen(cmd,stdout = PIPE) # 启动进程方式 print ( "%s start successful" % self .name) def __getPid( self ): p = Popen([ "pidof" , self .name],stdout = PIPE) # 获取pid方式 pid = p.stdout.read().strip() # 过滤pid return pid def stop( self ): pid = self .__getPid() if pid: os.kill( int (pid), 15 ) # 杀掉进程方式,pid为数值类型 if os.path.exists( self .__pidFile()): os.remove( self .__pidFile()) # 删掉pid文件 print ( "%s is stopped" % self .name) def restart( self ): self .stop() self .start() def status( self ): pid = self .__getPid() if pid: print ( "%s is running" % self .name) else : print ( "%s is not running" % self .name) def help ( self ): print ( "Usage: %s {start|stop|restart|status}" % __file__) def main(): name = "memcached" program = "/data/program/memcached/bin/memcached" workdir = "/var/tmp/memcached" pm = Process(name = name, program = program, workdir = workdir) # 传参,也可以按顺序只写实参 try : cmd = sys.argv[ 1 ] # 如启动参数有误,则抛出异常,并退出 except Exception as e: raise e 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 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦