0

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

[Unit] 
Description=rfsensor 
After=multi-user.target 
[Service] 
Type=simple 
Restart=always 
ExecStart=/usr/bin/python -u /home/pi/pep/rfsensor.py >> /home/pi/Documents/rfsensor.log 2>&1 & 
[Install] 
WantedBy=multi-user.target 

The script is called just fine and automatically runs after boot, but no log file 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!

asked Feb 26, 2024 at 0:36
2
  • @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. Commented Feb 26, 2024 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. Commented Feb 26, 2024 at 13:09

2 Answers 2

0

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.

answered Feb 26, 2024 at 11:19
0

Update your systemd service file and add

user=pi
group=pi

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

answered Feb 27, 2024 at 20:29
2
  • 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. Commented Feb 28, 2024 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. Commented Feb 28, 2024 at 21:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.