Last active
August 29, 2015 14:07
-
-
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.
This file contains 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
#!/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 |
This file contains 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 | |
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