Sunday, November 22, 2015

Run Python script in the background as a service in raspberry pi on boot

This is a simple service in Python and need it to start running in the background when the Raspberry Pi boots. Here Python script receive udp data in real time. The following init script makes getting a Python script (or e.g. a Perl script) to run when the Raspberry Pi boots fairly painless. Services are supposed to run as "daemons" which is quite complicated in Python. We use start-stop-daemon command to run our script in the background and basically deals with everything we need.

This is init file save it as myservice.sh at
 /etc/init.d   


 #!/bin/sh 
 ### BEGIN INIT INFO 
 # Provides:     myservice 
 # Required-Start:  $remote_fs $syslog 
 # Required-Stop:   $remote_fs $syslog 
 # Default-Start:   2 3 4 5 
 # Default-Stop:   0 1 6 
 # Short-Description: Put a short description of the service here 
 # Description:    Put a long description of the service here 
 ### END INIT INFO 
 # Change the next 3 lines to suit where you install your script and what you want to call it 
 DIR=/usr/local/bin/myservice 
 DAEMON=$DIR/myservice.py 
 DAEMON_NAME=myservice 
 # Add any command line options for your daemon here 
 DAEMON_OPTS="" 
 # This next line determines what user the script runs as. 
 # Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python. 
 DAEMON_USER=root 
 # The process ID of the script when it runs is stored here: 
 PIDFILE=/var/run/$DAEMON_NAME.pid 
 . /lib/lsb/init-functions 
 do_start () { 
   log_daemon_msg "Starting system $DAEMON_NAME daemon" 
   start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS 
   log_end_msg $? 
 } 
 do_stop () { 
   log_daemon_msg "Stopping system $DAEMON_NAME daemon" 
   start-stop-daemon --stop --pidfile $PIDFILE --retry 10 
   log_end_msg $? 
 } 
 case "$1" in 
   start|stop) 
     do_${1} 
     ;; 
   restart|reload|force-reload) 
     do_stop 
     do_start 
     ;; 
   status) 
     status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $? 
     ;; 
   *) 
     echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}" 
     exit 1 
     ;; 
 esac 
 exit 0 

Then create myservice.py file at /usr/local/bin/myservice . and also create output.txt file in the same directory. change permission by chmod 777 myservice.py


 import sys, struct  
 from socket import *  
 SIZE = 1024   # packet size  
 hostName = gethostbyname('0.0.0.0')  
 mySocket = socket( AF_INET, SOCK_DGRAM )  
 mySocket.bind((hostName,18736))  
 repeat = True  
 while repeat:  
   (data,addr) = mySocket.recvfrom(SIZE)  
   data = struct.unpack('d',data)  
   data=int(data[0])  
   file = open("output.txt", "w")  
   file.write(str(data))  
   file.close()  
To run this when raspberry pi boot
 sudo nano /etc/rc.local  
:

then add bellow line before exit line in rc.local 
 sudo /etc/init.d/myservice.sh start  



you can also manually start your Python script using the command
 sudo /etc/init.d/myservice.sh start  

check its status with the
 sudo /etc/init.d/myservice.sh status  

stop it with
 sudo /etc/init.d/myservice.sh stop   

No comments:

Post a Comment