#!/usr/bin/env python33
#coding:utf8
#description: this is nginx server in python33 on scripts
import sys,os,time
class nginxserver(object):
nginx_path = '/usr/local/nginx/sbin/nginx'
configfile = '/usr/local/nginx/conf/nginx.conf'
pidfile = '/usr/local/nginx/logs/nginx.pid'
nginx_start = '%s -c %s'%(nginx_path,configfile)
nginx_stop = r'kill -QUIT $(cat %s)' %pidfile
nginx_reload = 'kill -HUP $(cat %s)' %pidfile
nginx_log = 'kill -USR1 $(cat %s)' %pidfile
nginx_access = '/usr/local/nginx/logs'
def start(self):
if os.path.isfile(self.pidfile):
print('nginx is already running!')
else:
if os.system(self.nginx_start) == 0:
print('nginx starting is OK!')
print('nginx starting is error!')
def stop(self):
if os.system(self.nginx_stop) == 0:
print('nginx stop is OK!')
print('nginx stoping is error!')
print('nginx is not running!')
def reload(self):
if os.system(self.nginx_reload) == 0:
print('nginx reading is OK')
print('nginx reading is error')
print('nginx is not running')
def relog(self):
os.system('cd %s && mv access.log access.log.%s' %(self.nginx_access,time.strftime('%Y%m%d%H%M%S',time.localtime())))
if os.system(self.nginx_log) == 0:
print('nginx reloging is ok')
print('nginx reloging is error')
def restart(self):
self.stop()
self.start()
def status(self):
print('nginx in running!')
os.system('lsof -i :80')
if name == 'main':
server = nginxserver()
if hasattr(server,sys.argv[1]): #查找实例中是否有次方法
func = getattr(server,sys.argv[1]) #生成一个函数执行内存地址
func()
print("Usage: %s {start|stop|status|restart|reload|relog}" %sys.argv[0])
本文转自 80后小菜鸟 51CTO博客,原文链接:http://blog.51cto.com/zhangxinqi/2056483