Skip to content

Instantly share code, notes, and snippets.

@jamiesun
Created May 2, 2013 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiesun/5500909 to your computer and use it in GitHub Desktop.
Save jamiesun/5500909 to your computer and use it in GitHub Desktop.
使用python来写linux的service脚本
#!/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