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.

8
  • 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. Commented Jun 20, 2016 at 0: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? Commented Jun 20, 2016 at 0:23
  • @Mr.Llama I can confirm that the script is running as I am able to use the service. Commented Jun 20, 2016 at 0:25
  • @threadp When I call it from the terminal it works perfectly. As I call print() I think it uses stdout, doesn't it? Commented Jun 20, 2016 at 0: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 > ) Commented Jun 20, 2016 at 0:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
-u is exactly what I needed here. Scratching my head for ages as to why the file was empty until the process ended - thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.