-2

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 touched 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.

Community
  • 1
  • 1
Madivad
  • 2,999
  • 7
  • 33
  • 60
  • Why not just redirect the output to a log file and just tail that? – SheetJS Jul 27 '13 at 16:38
  • Thanks Nirk, I had thought of that, but the script provides a polling indicator to let me know its still alive, and although I haven't tried it, a tail would display a beeline for every dot that appears. `tail` would work for my finished script, but while I'm still building it, I need to see a live representation that its still running. – Madivad Jul 27 '13 at 16:44
  • Perhaps running the script as you are doing from rc.local, but redirecting the output of the script to /dev/console? – mti2935 Jul 27 '13 at 21:12
  • Thanks guys, @nirk, you hit the nail on the head, tail DOES work as needed (I was expecting a newline at the end of each line, but it doesn't). So all I'm doing is redirecting stdout and stderr to a log, I can then tail the log perfectly. If you put that in as an answer I can tick it. Thanks. – Madivad Jul 27 '13 at 23:40

1 Answers1

3

Instead of using screen, if you just want to see the output you should redirect the output of the command to a log file and then tail the file.

SheetJS
  • 22,470
  • 12
  • 65
  • 75
  • Thanks Nirk, I know I said above that that worked, it did from the command line, but not from within `/etc/rc.local`. The log file is zero bytes. but the process appears to be running (in `ps aux`). Any final thoughts? – Madivad Jul 27 '13 at 23:50
  • It may be run too early. Can you post the script you are running (and whether it depends on a service like mysql)? – SheetJS Jul 28 '13 at 01:32