I have a Python script with a function I want to execute every X minutes.
from threading import Timer
x = 5
def control():
Timer(x*60, control).start()
rest_of_the_function()
control()
When I run the script in its Python virtual environment from the terminal, there is no problem; when I run it as systemd service, it calls control()
function just once and then it doesn't do anything else.
If I check the service through systemctl status myservice
its Active field says active (running). There are no errors in the log.
The content of the /lib/systemd/system/myservice.service file is the following.
[Unit]
Description=Short description
[Service]
Type=simple
WorkingDirectory=/home/user/myservice
ExecStart=/home/user/myservice/myvenv/bin/python main.py
[Install]
WantedBy=multi-user.target
Why does it just call control()
once? Can I fix it?
start()
on theTimer
object? Did you check withsystemctl
that your python program is actually running and it didn't exit? – sebasth Oct 1 '17 at 16:11bash
aspython main.py
. I missed thestart()
here but I have it in my script (edited the post). How can I check if the python program is running? Only check I know is throughjournalctl -u myservice
and there is no exit code or anything like that. – VaNa Oct 1 '17 at 16:19systemctl status myservice
and look at theActive
field. – sebasth Oct 1 '17 at 16:22