Systemd : Run a Python Script At Startup (virtualenv)
Your unit file is correct. If you want to run any python file under an venv you just need to reference the python binary in the venv directory like you did with /home/user/anaconda3/bin/python
[Unit]
Description=My Script Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/home/user/anaconda3/bin/python /home/user/space/run.py
[Install]
WantedBy=multi-user.target
What sticks out is the reason your unit fails: code=exited, status=200/CHDIR
. This most likely indicates an issue within your script.
If you want to debug that, you would do the following:
- Run the command you added to
ExecStart=
exactly like that under root to see, if the issue is caused by your script. - If that runs without errors, look at the journal with
journalctl -u <unit_name>
. That should give you some more information on issues with your unit.
Post Scriptum
Both of the following [Service]
options work:
ExecStart=/home/user/anaconda3/bin/python /home/user/space/run.py
or
WorkingDirectory=/home/user/space
ExecStart=/home/user/anaconda3/bin/python run.py
The only difference is that relative calls in your script run from different directories. So if your script contains a line open("my_file", "w")
, in the first example it would create a file /my_file
and the second a file /home/user/space/my_file
.
Related videos on Youtube
Jasmine
Updated on September 18, 2022Comments
-
Jasmine over 1 year
I have a python script that I normally run it with this command:
(environment) python run.py
I want to run this script at start. (I'm using ubuntu) Here is my service:
[Unit] Description=My Script Service After=multi-user.target [Service] Type=idle ExecStart=/home/user/anaconda3/bin/python /home/user/space/run.py [Install] WantedBy=multi-user.target
BTW, I couldn't run this script, but I could run any script that is not inside environment. How can I run a python script at startup (virtualenv)?
sudo systemctl status user_sent ● user_sent.service - Mail Service Loaded: loaded (/lib/systemd/system/user_sent.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since xxxxx 16:30:20 MSK; 3s ago Process: 3713 ExecStart=/usr/bin/python run.py (code=exited, status=200/CHDIR) Main PID: 3713 (code=exited, status=200/CHDIR)
-
Federico Sierra over 7 yearsWhat say logs and status?
-
Jasmine over 7 yearsI have edited my question. Many thanks @FedericoSierra
-
Federico Sierra over 7 yearsThe script is runnig outside venv,
/usr/bin/python run.py
, see stackoverflow.com/questions/37211115/… and unix.stackexchange.com/questions/278188/… -
Jasmine over 7 yearsI did try them. I have to run "python run.py" not "python /user_sent/run.py"
-
Federico Sierra over 7 yearsThe python path must point to version inside virtual environment, Eg
/path/to/your/virtualenv/bin/python
-
rhasti over 7 yearsYou could setup Environment by starting virtualenv in 'ExecStartPre=' Section
-
Daniel over 7 yearsThe one thing that makes me wonder is this line:
code=exited, status=200/CHDIR
. The script seems to start fine, but something in the script seems to cause an error. What happens if you run/home/user/anaconda3/bin/python /home/user/space/run.py
as root? -
Thomas over 7 yearsTry to convert
Type=idle
toType=oneshot
. -
Jasmine over 7 years@Daniel this python script only run with "python run.py" from application directory. "WorkingDirectory=/home/user/space ExecStart=/home/user/anaconda3/bin/python run.py Restart=always" doesnt work.
-
Jasmine over 7 yearsyes, they run as root
-
Daniel over 7 yearsThat was not what I asked. Please run the full command as root.
/home/user/anaconda3/bin/python /home/user/space/run.py
-
Jasmine over 7 yearsfull command run. But the "run.py" must run as "python run.py" not full path.
-
Jasmine over 7 yearsfull command working with "cd space; /home/user/anaconda3/bin/python run.py"
-
Daniel over 7 yearsLet us continue this discussion in chat.
-