• We did not send an email asking for donations - please read this post.

Python script as a systemd service

Saitama

New Member
Joined
Jun 14, 2022
Messages
3
Reaction score
1
Credits
34
Hi community,

I'm running a python script as a systemd service. The task of the script is extremely simple: check the last few lines of the file for changes. However, the problem is that for some reason, when the script is running as a systemd service, nothing happens, what I mean, is that the file is not read, and if there are any changes, the script does not react to it in any way. I would like to note that in the local environment everything works as it should, after running the script, the file is scanned for changes and if they are found, the action is performed.

Has anyone experienced this and can advise what the problem is?

In the script, I use the 'while True' loop. And here's how the .service file looks like:

```
[Unit]
Description=Look for changes
After=multi-user.target

[Service]
Type=simple
Restart=always
Environment="FILE=/path/to/script/file.txt"
WorkingDirectory=/path/to/script
ExecStart=/usr/bin/python3 /path/to/script/script.py

[Install]
WantedBy=multi-user.target
```
 


Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,472
Reaction score
1,195
Credits
10,700
Did you enable it?
Code:
sudo systemctl enable my.service
You might need to reload systemd daemon, too.
Code:
sudo systemctl daemon-reload
Also, the service file should be placed in /etc/systemd/system/
 
OP
S

Saitama

New Member
Joined
Jun 14, 2022
Messages
3
Reaction score
1
Credits
34
Hi Tolkem,

Thank you for your reply! Yes, I have enabled service and restarted the daemon and my service.
That's correct, .service file is located in /etc/systemd/system/
 
OP
S

Saitama

New Member
Joined
Jun 14, 2022
Messages
3
Reaction score
1
Credits
34
I’ve found the main cause of the issue. It was because service environment var (‘FILE’) was described in two places and it’s value was different:
  1. /etc/systemd/system/MY-SERVICE.service; (FILE=value1)
  2. /etc/systemd/system/MY-SERVICE.service.d/override.conf (a unit snippet override file); (FILE=value2)
I had to remove ‘Environment’ from ‘/etc/systemd/system/MY-SERVICE.service’ and keep it only in the ‘override.conf’ file.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top