简体   繁体   中英

I have a problem with running a python script as a systemd service

I have a reminder app and i want to run it as a service. I created a service file called 'reminder_py.service' under '/lib/systemd/system'. commands inside 'reminder_py.service':

[Unit]
Description=Dummy Service
After=multi-user.target
Conflicts=getty@tty1.service

[Service]
Type=simple
Username=hrx
Groupname=hrx
ExecStart=/usr/bin/python3 /home/hrx/reminder/reminder.py
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

And I tried to start it with the following commands:

sudo systemctl daemon-reload
sudo systemctl enable reminder_py.service
sudo systemctl start reminder_py.service

But the sudo systemctl status reminder_py.service command says:

hrx@X230:/lib/systemd/system$ sudo systemctl status reminder_py.service
● reminder_py.service - Dummy Service
     Loaded: loaded (/lib/systemd/system/reminder_py.service; enabled; vendor p>
     Active: failed (Result: exit-code) since Sat 2020-08-15 23:59:06 +03; 2min>
    Process: 13952 ExecStart=/usr/bin/python3 /home/hrx/reminder/reminder.py (c>
   Main PID: 13952 (code=exited, status=2)

Aug 15 23:59:06 X230 systemd[1]: Started Dummy Service.
Aug 15 23:59:06 X230 systemd[1]: reminder_py.service: Main process exited, code>
Aug 15 23:59:06 X230 systemd[1]: reminder_py.service: Failed with result 'exit->

How can I solve this problem?

Maybe the following can help:

hrx@X230:/lib/systemd/system$ users
hrx

hrx@X230:/lib/systemd/system$ groups
hrx adm cdrom sudo dip plugdev lpadmin sambashare


hrx@X230:/lib/systemd/system$ ls -al | grep reminder_py.service 
-rwxrwxrwx  1 root root   256 Aug 15 23:55 reminder_py.service

And I found a comment from another site that could help me. But I don't quite understand what he's saying.

A fine and simple to follow tutorial. I would add a couple of caveats – with the.service file as written the python script will be run as root and this may have unintended consequences. Also the environment will be different from that for a normal user. To fix add the lines User=username and Group=groupname before the ExecStart line. To add environment variables expected by the script add the line Environment=”variable_name=variable_value” before the ExecStart line.

I solved the problem by replacing contents of the file with:

[Unit]
Description=Reminder App
After=multi-user.target
Conflicts=getty@tty1.service

[Service]
Type=simple
ExecStart=/home/hrx/anaconda3/bin/python /home/hrx/Desktop/reminder/reminder.py
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

To learn the path of python run this command:

type -a python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM