11

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)
Jasmine
  • 243
  • 1
  • 3
  • 8
  • 1
    What say logs and status? – Federico Sierra Dec 19 '16 at 13:22
  • I have edited my question. Many thanks @FedericoSierra – Jasmine Dec 19 '16 at 13:35
  • 1
    The script is runnig outside venv, `/usr/bin/python run.py`, see http://stackoverflow.com/questions/37211115/how-to-enable-a-virtualenv-in-a-systemd-service-unit and http://unix.stackexchange.com/questions/278188/systemd-how-to-start-stop-python-script-that-should-run-in-background-inside-v – Federico Sierra Dec 19 '16 at 13:39
  • I did try them. I have to run "python run.py" not "python /user_sent/run.py" – Jasmine Dec 19 '16 at 13:47
  • 1
    The python path must point to version inside virtual environment, Eg `/path/to/your/virtualenv/bin/python` – Federico Sierra Dec 19 '16 at 13:59
  • You could setup Environment by starting virtualenv in 'ExecStartPre=' Section – rhasti Dec 19 '16 at 14:12
  • @rhasti no need for that. It is enough to run python with the full path to the venv like Federico explained. – Daniel Dec 19 '16 at 17:13
  • 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? – Daniel Dec 19 '16 at 17:15
  • Try to convert `Type=idle` to `Type=oneshot`. – Thomas Dec 19 '16 at 20:13
  • @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 Dec 20 '16 at 09:53
  • That was not what I asked. – Daniel Dec 20 '16 at 09:57
  • yes, they run as root – Jasmine Dec 20 '16 at 10:02
  • That was not what I asked. Please run the full command as root. `/home/user/anaconda3/bin/python /home/user/space/run.py` – Daniel Dec 20 '16 at 10:11
  • full command run. But the "run.py" must run as "python run.py" not full path. – Jasmine Dec 20 '16 at 10:28
  • full command working with "cd space; /home/user/anaconda3/bin/python run.py" – Jasmine Dec 20 '16 at 10:30
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/50369/discussion-between-daniel-and-phpexe). – Daniel Dec 20 '16 at 10:59

1 Answers1

14

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.

Daniel
  • 6,780
  • 5
  • 31
  • 60