I have a python script that I want run prior to any user logging in. This is for a home automation server and I want it always to be up and running as soon as the system allows.
I already have it in the rc.local
file including an ampersand. This works.
But I can't see the screen output that it produces.
When I log into the unit (it's a raspberry pi running raspian) via SSH I can start it using screen
which works the best as when I logout and back in, it's still there. AND I can see the output from the script.
But when I try running screen
from the rc.local
file, and subsequently login to check, the script isn't there (ie ps aux | grep script.py
confirms)
edit: I've taken on Nirk's solution below about using tail
. From the command line, it works fine. But starting it form within /etc/rc.local
doesn't. I have touch
ed the file and everyone has write access to it.
This is what's in my rc.local
file:
python /home/pi/gateway.py &> /x10.log &
UPDATE This is how I did it in the end:
Although the question was just about how to run in the background prior to login, there was more to it. The script is a work in progress and because of the way a particular serial device acts with it, it is/was prone to crashing (I've almost got all the bugs out of it). I needed to be able to restart it as well. I tried nohup
but for some reason, it wouldn't keep it alive so in the end I found the top answer from this page got it all sorted.
In my /etc/rc.local
I included a shell script to run:
nohup /home/pi/alwaysrun.sh > /home/pi/mha.log 2>&1 &
alwaysrun.sh
contains:
#!/bin/bash
until python /home/pi/gateway.py; do
echo "'gateway.py' exited with exit code $?. Restarting..." >&2
sleep 1
done
nohup
will keep the alwaysrun.sh
script alive, and that in turn keeps my gateway.py
script running. The redirect of stdout and stderr means I can setup a tail (and/or go back and check) the log.