Created
October 4, 2012 09:52
-
-
Save stevommmm/3832634 to your computer and use it in GitHub Desktop.
klaus init.d script
This file contains hidden or 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/bin/python | |
### BEGIN INIT INFO | |
# Provides: klaus | |
# Required-Start: | |
# Required-Stop: | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Git GUI loader | |
# Description: Start klaus git web front end | |
# | |
### END INIT INFO | |
import sys | |
import os | |
def start(): | |
if os.path.exists(pid): | |
print "Already started!" | |
sys.exit(1) | |
newpid = os.fork() | |
if newpid > 0: | |
sys.exit(0) | |
os.chdir('/') | |
os.umask(0) | |
os.setsid() | |
newpid = os.fork() | |
if newpid > 0: | |
sys.exit(0) | |
f = open(pid, 'w+') | |
f.write(str(os.getpid())) | |
f.close() | |
os.chdir(dirl) | |
sys.stdin = open(os.devnull, 'r') | |
sys.stderr = open(os.devnull, 'a+') | |
sys.stdout = open(os.devnull, 'a+') | |
os.dup2(sys.stdin.fileno(), 0) | |
os.dup2(sys.stdout.fileno(), 1) | |
os.dup2(sys.stderr.fileno(), 2) | |
os.execv(cmd[0], cmd[1:]) | |
def stop(): | |
if not os.path.exists(pid): | |
print "Not started!" | |
exit(1) | |
f = open(pid, 'r') | |
oldpid = f.read() | |
f.close() | |
try: | |
os.kill(int(oldpid), 9) | |
except OSError, error: | |
print str(error) | |
os.remove(pid) | |
if __name__ == "__main__": | |
if not len(sys.argv) > 1: | |
exit(1) | |
if not sys.argv[1]: | |
exit(1) | |
command = sys.argv[1] | |
pid = '/var/run/klaus.pid' | |
dirl = '/home/git/klaus' | |
repos = '/home/git/repositories/' | |
cmd = ['/usr/bin/env', 'python', './bin/klaus', '--host', '192.168.0.13', '--sitename', 'localgit'] | |
cmd.extend([repos + x for x in os.listdir(repos)]) | |
if command == "start": | |
start() | |
elif command == "stop": | |
stop() | |
elif command == "restart": | |
stop() | |
start() | |
else: | |
print "Unknown command" | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment