Systemd : Run a Python Script At Startup (virtualenv)

30,522

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:

  1. Run the command you added to ExecStart= exactly like that under root to see, if the issue is caused by your script.
  2. 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.

Share:
30,522

Related videos on Youtube

Jasmine
Author by

Jasmine

Updated on September 18, 2022

Comments

  • Jasmine
    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
      Federico Sierra over 7 years
      What say logs and status?
    • Jasmine
      Jasmine over 7 years
      I have edited my question. Many thanks @FedericoSierra
    • Federico Sierra
      Federico Sierra over 7 years
      The script is runnig outside venv, /usr/bin/python run.py, see stackoverflow.com/questions/37211115/… and unix.stackexchange.com/questions/278188/…
    • Jasmine
      Jasmine over 7 years
      I did try them. I have to run "python run.py" not "python /user_sent/run.py"
    • Federico Sierra
      Federico Sierra over 7 years
      The python path must point to version inside virtual environment, Eg /path/to/your/virtualenv/bin/python
    • rhasti
      rhasti over 7 years
      You could setup Environment by starting virtualenv in 'ExecStartPre=' Section
    • Daniel
      Daniel over 7 years
      The 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
      Thomas over 7 years
      Try to convert Type=idle to Type=oneshot.
    • Jasmine
      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
      Jasmine over 7 years
      yes, they run as root
    • Daniel
      Daniel over 7 years
      That was not what I asked. Please run the full command as root. /home/user/anaconda3/bin/python /home/user/space/run.py
    • Jasmine
      Jasmine over 7 years
      full command run. But the "run.py" must run as "python run.py" not full path.
    • Jasmine
      Jasmine over 7 years
      full command working with "cd space; /home/user/anaconda3/bin/python run.py"
    • Daniel
      Daniel over 7 years