I'm trying to invoke a python script as a service AND send it's output to a logfile. I created the following file; /etc/systemd/system/rfsensor.service, with contents:

ExecStart=/usr/bin/python -u /home/pi/pep/rfsensor.py >> /home/pi/Documents/rfsensor.log 2>&1 &

The script is called just fine and automatically runs after boot, but no logfile is written. I also tried wrapping it in a bash script, but that didn't work either. Am I trying to do something that isn't supported in a service by redirecting output to a file? Is there another way to achieve this when running as a service? Thanks!

  • @Milliways that explains the redirect issue, thank you. Many people run python scripts as services. In my case it makes it really easy and robust for other scripts to stop and start rfsensor.py without knowing its PID. rfsensor.py is controlling an IoT gateway, parsing and acting on a network of RF sensors reporting in. One of those actions requires another script to briefly stop rfsensor.py, take control of the sensors, and then restart rfsensor.py. OS is Debian on a Pi.
    – NigelS
    Commented Feb 26 at 13:09
  • If there's a more elegant way for one script to find and stop another script, and then restart it with a redirect, I'd be glad to find out.
    – NigelS
    Commented Feb 26 at 13:09

2 Answers 2


It is unclear what you are trying to do. What does this script do?

systemd runs as root (and is normally used to start system processes).
It does not run in a shell (so you can't redirect); if you used it to start a shell it might work.

You appear to be trying to run something owned by pi - if it is sensible for root to run it root should own the code.

Depending on what your mystery script does you should try running it as a cron job for user pi.

You don't say what OS so there are more imponderables.


Update your systemd service file and add


That will get the service running with the correct userid and group. That should help fix at least one of your errors.

  • DId that and wrapped the python call in a bash script as talked about here: squash.io/executing-bash-script-at-startup-in-ubuntu-linux/…. But that didn't start the python script unfortunately.
    – NigelS
    Commented Feb 28 at 0:58
  • sudo systemctl enable foo.service; sudo systemctl start foo.service will get the service started and enable it for auto start at the next boot.
    – Dougie
    Commented Feb 28 at 21:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.