Last active
February 7, 2019 10:30
-
-
Save MarcoCiaramella/0035ac8f22f06be909b7e1cd3890dfeb to your computer and use it in GitHub Desktop.
System utility written in python that create a background service from executable. Service created will be run as: sudo service MyService {start|stop|restart|status}
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
######### | |
# System utility written in python that create a background service from executable. | |
# Service created will be run as: sudo service MyService {start|stop|restart|status} | |
# Run: sudo python addSystemService.py [executable name] [service name] [user] | |
# Example: sudo python addSystemService.py ./project/my_project.py MyPorjectService root options | |
######### | |
from sys import argv | |
import os | |
import stat | |
import subprocess | |
import platform | |
script = '#!/bin/sh\n\ | |
\n\ | |
### BEGIN INIT INFO\n\ | |
# Provides: %s\n\ | |
# Required-Start: $remote_fs $syslog\n\ | |
# Required-Stop: $remote_fs $syslog\n\ | |
# Default-Start: 2 3 4 5\n\ | |
# Default-Stop: 0 1 6\n\ | |
# Short-Description: Put a short description of the service here\n\ | |
# Description: Put a long description of the service here\n\ | |
### END INIT INFO\n\ | |
\n\ | |
# Change the next 3 lines to suit where you install your script and what you want to call it\n\ | |
DIR=%s\n\ | |
DAEMON=%s\n\ | |
DAEMON_NAME=%s\n\ | |
\n\ | |
# Add any command line options for your daemon here\n\ | |
DAEMON_OPTS=%s\n\ | |
\n\ | |
# This next line determines what user the script runs as.\n\ | |
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.\n\ | |
DAEMON_USER=%s\n\ | |
\n\ | |
# The process ID of the script when it runs is stored here:\n\ | |
PIDFILE=/var/run/$DAEMON_NAME.pid\n\ | |
\n\ | |
. /lib/lsb/init-functions\n\ | |
\n\ | |
do_start () {\n\ | |
log_daemon_msg "Starting system $DAEMON_NAME daemon"\n\ | |
echo "RUN: start-stop-daemon --start --background --chdir $DIR --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS"\n\ | |
start-stop-daemon --start --background --chdir $DIR --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS\n\ | |
echo "RETURN CODE: $?"\n\ | |
log_end_msg $?\n\ | |
}\n\ | |
do_stop () {\n\ | |
log_daemon_msg "Stopping system $DAEMON_NAME daemon"\n\ | |
echo "RUN: start-stop-daemon --stop --pidfile $PIDFILE --retry 10"\n\ | |
start-stop-daemon --stop --pidfile $PIDFILE --retry 10\n\ | |
echo "RETURN CODE: $?"\n\ | |
log_end_msg $?\n\ | |
while pgrep -x $DAEMON > /dev/null; do sleep 2; done\n\ | |
echo "$DAEMON_NAME stopped"\n\ | |
}\n\ | |
\n\ | |
case "$1" in\n\ | |
\n\ | |
start|stop)\n\ | |
do_${1}\n\ | |
;;\n\ | |
\n\ | |
restart|reload|force-reload)\n\ | |
do_stop\n\ | |
do_start\n\ | |
;;\n\ | |
\n\ | |
status)\n\ | |
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?\n\ | |
;;\n\ | |
\n\ | |
*)\n\ | |
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"\n\ | |
exit 1\n\ | |
;;\n\ | |
\n\ | |
esac\n\ | |
exit 0\n\ | |
' | |
if len(argv) < 5: | |
print "Use: python addSystemService.py [executable] [service] [user] [options]" | |
exit() | |
executable = os.path.abspath(argv[1]) | |
service = argv[2] | |
user = argv[3] | |
options = argv[4] | |
if platform.linux_distribution()[0] == 'Ubuntu': | |
update_cmd = ['update-rc.d', service, 'defaults'] | |
#elif platform.linux_distribution()[0] == 'CentOS': | |
# update_cmd = ['chkconfig', '--add', service] | |
else: | |
print 'Distribution not supported' | |
exit() | |
dirExecutable = os.path.dirname(executable) | |
executable = os.path.basename(executable) | |
s = open('/etc/init.d/'+service,'w') | |
s.write(script%(service,dirExecutable, executable, executable.split('.')[0], options, user)) | |
s.close() | |
os.chmod('/etc/init.d/'+service, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR) | |
subprocess.call(update_cmd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment