#!/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!')
else:
print('nginx starting is error!')
def stop(self):
if os.path.isfile(self.pidfile):
if os.system(self.nginx_stop) == 0:
print('nginx stop is OK!')
else:
print('nginx stoping is error!')
else:
print('nginx is not running!')
def reload(self):
if os.path.isfile(self.pidfile):
if os.system(self.nginx_reload) == 0:
print('nginx reading is OK')
else:
print('nginx reading is error')
else:
print('nginx is not running')
def relog(self):
if os.path.isfile(self.pidfile):
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')
else:
print('nginx reloging is error')
else:
print('nginx is not running')
def restart(self):
self.stop()
self.start()
def status(self):
if os.path.isfile(self.pidfile):
print('nginx in running!')
os.system('lsof -i :80')
else:
print('nginx is not running!')
if name == 'main':
server = nginxserver()
if hasattr(server,sys.argv[1]): #查找实例中是否有次方法
func = getattr(server,sys.argv[1]) #生成一个函数执行内存地址
func()
else:
print("Usage: %s {start|stop|status|restart|reload|relog}" %sys.argv[0])