zoukankan      html  css  js  c++  java
  • nginx启动脚本(class练习)

    说明:使用类的方式编写程序启动脚本(练习)

    1
    #!/usr/bin/env python 2 3 import sys 4 import os 5 from subprocess import Popen,PIPE 6 7 class Process(object): 8 9 def __init__(self,name,pfile,workdir): 10 self.name = name 11 self.pfile = pfile 12 self.workdir = workdir 13 self._init() 14 15 def _init(self): 16 if os.path.exists(self.workdir): 17 os.chdir(self.workdir) 18 else: 19 os.makedirs(self.workdir) 20 os.chdir(self.workdir) 21 22 def _pidfile(self): 23 return os.path.join(self.workdir,self.pfile) 24 25 def _writepid(self): 26 if self.pid: 27 with open(self.pfile,'w') as fd: 28 fd.write('%s' % self.pid) 29 30 def _getpid(self): 31 p = Popen(['pidof','nginx'],stdout=PIPE) 32 return p.stdout.read() 33 34 def start(self): 35 cmd = self.name 36 if self._getpid(): 37 sys.exit('The %s are started!!' % self.name) 38 else: 39 p = Popen(cmd,stdout=PIPE,shell=True) 40 self.pid = (p.pid + 1) 41 self._writepid() 42 print('The %s is start ' % self.name) 43 44 def stop(self): 45 if self._getpid(): 46 for pid in self._getpid().strip().split(): 47 os.kill(int(pid),15) 48 if os.path.exists(self._pidfile()): 49 os.remove(self._pidfile()) 50 print('The %s is stopped... ' % self.name) 51 else: 52 print('The %s is not running...' % self.name) 53 54 def restart(self): 55 self.stop() 56 self.start() 57 58 def status(self): 59 if self._getpid(): 60 print('The %s status is running...' % self.name) 61 else: 62 print('The %s status is stopped ...' % self.name) 63 64 def reload(self): 65 os.system('%s -s reload' % self.name) 66 print('The %s is reload Successful !' % self.name ) 67 68 def help(self): 69 print('Usage: %s {start|stop|restart|reload}' % __file__) 70 71 def main(): 72 name = '/usr/sbin/nginx' 73 workdir = '/var/run/nginx' 74 pfile = 'nginx.pid' 75 nginx = Process(name,pfile,workdir) 76 try: 77 para = sys.argv[1] 78 except IndexError as e: 79 sys.exit(e) 80 if para == 'start': 81 nginx.start() 82 elif para == 'stop': 83 nginx.stop() 84 elif para == 'restart': 85 nginx.restart() 86 elif para == 'status': 87 nginx.status() 88 elif para == 'reload': 89 nginx.reload() 90 else: 91 nginx.help() 92 93 94 if __name__ == '__main__': 95 main()
  • 相关阅读:
    Python
    git SSL certificate problem: unable to get local issuer certificate
    Chapter 1 Securing Your Server and Network(13):配置端点安全性
    例说linux内核与应用数据通信系列
    Android 学习历程摘要(三)
    线程调度策略SCHED_RR(轮转法)和SCHED_FIFO(先进先出)之对照
    内核工作队列【转】
    android 电池(三):android电池系统【转】
    android 电池(二):android关机充电流程、充电画面显示【转】
    android 电池(一):锂电池基本原理篇【转】
  • 原文地址:https://www.cnblogs.com/dachenzi/p/6790013.html
Copyright © 2011-2022 走看看