3

I am starting my service running on a Raspberry Pi 2 (Raspbian) using a command in rc.local which looks like this:

python3.4 /home/pi/SwitchService/ServiceStart.py >/home/pi/SwitchService/log &
python3.4 /home/pi/test.py >/home/pi/log2 &

For some reason I don't see any text in the log file of my service although the script prints to stdout.

the two scripts look like this:

test.py

print("Test")

ServiceStart.py

from Server import Server
print("Test")
if __name__ == "__main__":
    server = Server()

Because I couldn't get the bash solution to work I tried this other solution whether that works for me. It behaves exactly the same like the bash based method. So my service writes nothing to the log file although the empty file is created.

Community
  • 1
  • 1
ThunderStorm
  • 766
  • 8
  • 24
  • 2
    Are you sure the script is actually running? Startup routines don't usually have `PATH` set, so it may not know where `python3.4` is located without its full path. – Mr. Llama Jun 20 '16 at 00:23
  • Are you sure that ServiceStart is writing to stdout? Or is it writing to stderr? What happens when you dont daemonize it? Does it print anything? To which standard file? Stdout or stderr? – pah Jun 20 '16 at 00:23
  • @Mr.Llama I can confirm that the script is running as I am able to use the service. – ThunderStorm Jun 20 '16 at 00:25
  • @threadp When I call it from the terminal it works perfectly. As I call print() I think it uses stdout, doesn't it? – ThunderStorm Jun 20 '16 at 00:25
  • @ThunderStorm Can you also redirect the stderr for debugging purposes? `python3.4 /home/pi/SwitchService/ServiceStart.py >& /home/pi/SwitchService/log &` (note the `>&` instead the `>` ) – pah Jun 20 '16 at 00:26
  • @threadp When I print to stderr and redirect it not even an empty file is created. – ThunderStorm Jun 20 '16 at 00:36
  • That sounds wrong... even if the script outputs nothing, the shell itself should have created the output file. Could you check [the following locations](http://askubuntu.com/a/434309/319555) for errors? – Mr. Llama Jun 20 '16 at 00:43
  • Same problem if i run a script python .py > TestFile.txt all the output disappears from the terminal output the file TestFile.txt is created but it's totally empty. Only seems to happen with python. When I control C i see some output sometimes but not always. This is definitely a python issue. – Owl Jun 20 '19 at 09:34

1 Answers1

3

First, make sure that your script is actually running. Many schedulers and startup routines don't have PATH set, so it may not be finding python3.4. Try modifying the command to include the full path (e.g. /full/path/python3.4).

Secondly, it's not recommended to include long running scripts in rc.local without running them in the background (the documentation even states this). The Raspberry Pi waits for the commands to finish before continuing to boot, so if it runs forever, your Raspberry Pi may never finish booting.

Lastly, assuming the previous two issues have been taken care of, make sure that your program isn't buffering output too aggressively. You can try flushing stdout to see if that helps.

Community
  • 1
  • 1
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • 4
    Thanks for your answer. I took care of the first two problems. The solution was flushing stdout (although that seems quite weird to do each time). In my case I added `-u` to the python script call. – ThunderStorm Jun 20 '16 at 00:43
  • -u is exactly what I needed here. Scratching my head for ages as to why the file was empty until the process ended - thanks! – ATG Dec 03 '21 at 12:01