1

I am trying to run my Python script as a system service. For this I have installed all necessary libraries in a virtualenv and have created the following service in /usr/lib/systemd/system:

[Unit]
Description=Desc
After=network.target

[Service]
Type=simple
User=<my-user>
Restart=on-abort
WorkingDirectory=/home/<my-user>
ExecStart=/home/<my-user>/.virtualenvs/myvenv/bin/python /home/<my-user>/workspacePython/test.py
Environment="PATH=/home/<my-user>/.virtualenvs/myvenv/bin"

[Install]
WantedBy=multi-user.target

But when I try to run it, it fails with:

Okt 07 20:45:49 fedora systemd[40808]: test.service: Failed at step EXEC spawning /home/<my-user>/.virtualenvs/myvenv/bin/python: Permission denied

The access rights for the python binary are rather permissive (777), so I am a bit confused how this comes about.

My Question is: How do I get to start the script within the virtualenv with systemd?

WayneNani
  • 173
  • 1
  • 9
  • Does your test.py also has 777 permission? – dsenese Oct 07 '21 at 19:35
  • What a coincidence! I struggled with a similar issues for days (`celery service`) and just figured out what what I missed. User and group defined in the `.service` where not granted the permissions on the script I wanted to start. `systemctl status x.service` gave me `status=126` (unfortunately I oversaw that at first), which lead me to a [stackoverflow Q&A](https://stackoverflow.com/questions/9979251/error-with-bash-script-exit-code-126) helped me to finally solve the issue. – marcel h Oct 07 '21 at 19:42
  • The same error comes up when I just try to launch the python binary without a script, so this does not seem to be the issue. In both cases, it exits with code 203 – WayneNani Oct 08 '21 at 08:28

1 Answers1

0

I had a similar issue on a service I created this morning.

Systemd does not seem to like WorkingDirectory=/home/<my-user> or absolute paths including /home in either case it will fail with the obscure "Permission denied" error.

The fix is to either replace references of "/home/" with ~ or move things outside the home path e.g /opt/<app-name>.

carbontwelve
  • 1,090
  • 1
  • 13
  • 24