Skip to content

Instantly share code, notes, and snippets.

@danechitoaie
Created November 16, 2015 23:00
Show Gist options
  • Save danechitoaie/f2bb03fd0c2d6b803878 to your computer and use it in GitHub Desktop.
Save danechitoaie/f2bb03fd0c2d6b803878 to your computer and use it in GitHub Desktop.
Virtualenv Systemd Service
[Unit]
Description=Python Virtualenv service example
After=network.target
[Service]
User=my_user
Group=my_group
Environment=VIRTUAL_ENV=/home/my_user/.virtualenvs/my_venv
Environment=PATH=$VIRTUAL_ENV/bin:$PATH
ExecStart=$VIRTUAL_ENV/bin/my_entrypoint
Restart=always
[Install]
WantedBy=multi-user.target
@h3ndrk
Copy link

h3ndrk commented Jun 11, 2017

@jcaesar you are right: I'm getting the following error (when trying to start the unit):

systemd[1]: Executable path is not absolute, ignoring: $VIRTUAL_ENV/bin/my_entrypoint
systemd[1]: Service lacks both ExecStart= and ExecStop= setting. Refusing.

So a fix would be:

- ExecStart=$VIRTUAL_ENV/bin/my_entrypoint
+ ExecStart=/home/my_user/.virtualenvs/my_venv/bin/my_entrypoint

See also: https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart=

@Digenis
Copy link

Digenis commented May 27, 2020

Systemd does not expand environment variables.
You end up with literal $VIRTUAL_ENV and $PATH inside the PATH variable
and in path to the executable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment