The major problem with most init style scripts is they run lots of processes and can be a mess to get working the first time: here is a plug n' play solution, just replace the ARGS with the program you want to run and the program's arguments. In the provided example, it opens chromium (the open source version of Google Chrome) in kiosk mode to this site (press Alt + F4 to exit).
#!/usr/bin/env python
'''
2012-01-15 Joseph Lewis <joehms22@gmail.com>
Provides init-like handling of a program.
'''
import os
import sys
import signal
from subprocess import Popen
ARGS = ["chromium-browser", "--kiosk", "http://onehourhacks.blogspot.com"]
PIDFILE = "/tmp/%s.pid" % (ARGS[0])
try:
cmd = sys.argv[1]
except IndexError, e:
print("Usage: %s (start|stop|restart)" % (sys.argv[0]))
exit(2)
def start():
try:
j = open(PIDFILE)
j.close()
print("Already running!")
return
except IOError:
pid = Popen(ARGS).pid
j = open(PIDFILE, 'w')
j.write(str(pid))
j.close()
def stop():
try:
j = open(PIDFILE)
os.kill(int(j.read()),signal.SIGTERM)
j.close()
os.remove(PIDFILE)
except IOError, e:
print("Cannot stop: the program is not running")
except OSError, e:
os.remove(PIDFILE)
print("The program has quit before it was killed")
# Very simple stuff here, initv style!
if cmd.lower() == "start":
start()
if cmd.lower() == "stop":
stop()
if cmd.lower() == "restart":
print("Restarting")
stop()
start()
No comments:
Post a Comment