Created
May 2, 2013 08:23
-
-
Save jamiesun/5500909 to your computer and use it in GitHub Desktop.
使用python来写linux的service脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python2.7 | |
#encoding:utf-8 | |
# | |
# chkconfig: - 91 35 | |
# description: Starts and stops the app server\ | |
# used to provide app services. | |
# | |
import sys | |
import os | |
pid = "/var/run/app.pid" | |
python_exec = '/usr/local/bin/python2.7' | |
app_dir = "/home/xxx/app" | |
app_script = "main.py" | |
def start(): | |
os.system("cd %s && exec nohup %s %s &"%(app_dir,python_exec,app_script)) | |
def stop(): | |
os.system("kill %s"%open(pid,'rb').read().strip()) | |
os.remove(pid) | |
def restart(): | |
stop() | |
start() | |
if __name__ == "__main__": | |
if len(sys.argv) == 2: | |
if 'start' == sys.argv[1]: | |
start() | |
elif 'stop' == sys.argv[1]: | |
stop() | |
elif 'restart' == sys.argv[1]: | |
restart() | |
else: | |
print "Unknown command" | |
sys.exit(2) | |
else: | |
print "usage: %s start|stop|restart" % sys.argv[0] | |
sys.exit(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment