Skip to content

Instantly share code, notes, and snippets.

@jonathlt
Last active August 29, 2015 14:07
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 jonathlt/957f6f46224be85f8d73 to your computer and use it in GitHub Desktop.
Save jonathlt/957f6f46224be85f8d73 to your computer and use it in GitHub Desktop.
Set up a python script to be called on startup of the machine, the script can be started and stopped using sudo testservice start | stop. Script should be copied to /etc/init.d and made executable.
#!/bin/bash
# /etc/init.d/testservice
# Provides: testservice
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This service is used to start a python script
### END INIT INFO
case "$1" in
start)
echo "Starting testservice"
/home/pi/python/service.py &
;;
stop)
echo "Stopping testservice"
pid=`ps -ef | grep '[p]ython /home/pi/python/service.py' | awk '{ print $2 }'`
echo $pid
kill $pid
sleep 2
echo "Service killed."
;;
*)
echo "Usage: /etc/init.d/testservice start|stop"
exit 1
;;
esac
exit 0
#!/usr/bin/python
import logging
import time
logging.basicConfig(filename='service.log',level=logging.INFO, format='%(asctime)s %(message)s')
var = 1
while var==1:
logging.info("log message")
time.sleep(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment