Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to create a systemd service for python script with virtualenv
[Unit]
Description=Some description
After=network.target
[Service]
Type=simple
User=user
WorkingDirectory=/home/user/somedir
Environment=PYTHONPATH=/home/user/somedir
ExecStart=/home/user/venv/bin/python script.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
@ricferr

This comment has been minimized.

Copy link
Owner Author

@ricferr ricferr commented Jun 25, 2018

Put these in /etc/systemd/system/

@shoguevara

This comment has been minimized.

Copy link

@shoguevara shoguevara commented Oct 11, 2019

This one is tested and works
Start python app from venv with systemd
Example
venv location - /home/path/to/venv

command to start the app:
source /home/path/to/venv/bin/activate
python app.py

systemd service config:
/etc/systemd/system/some.service

[Unit]
Description="Description"

[Service]
User=myUser
Group=myGroup
WorkingDirectory=/home/path/to/
VIRTUAL_ENV=/home/path/to/venv
Environment=PATH=$VIRTUAL_ENV/bin:$PATH
ExecStart=/home/path/to/venv/bin/python app.py
Restart=on-failure

[Install]
WantedBy=multi-user.target

@dwesolowski

This comment has been minimized.

Copy link

@dwesolowski dwesolowski commented Feb 9, 2020

How do we then deactivate with ExecStop?

@shoguevara

This comment has been minimized.

Copy link

@shoguevara shoguevara commented Feb 10, 2020

How do we then deactivate with ExecStop?

Sorry, needs additional digging - unfortunately I'm not an expert in a field, just achieved this result and shared it here. Moved far away from the issue since then.

@Arregator

This comment has been minimized.

Copy link

@Arregator Arregator commented Mar 27, 2020

I found that on the left side of [Service] section you may put only pre-defined in systemd.service(5) man page (type man 5 systemd.service for more details), so I changed a bit you definition to avoid error messages:

[Unit]
Description=HTTP Python server at http://<you_ip_adress>:8000
Documentation=https://docs.python.org/3.7/library/http.server.html
After=network.target

[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/usr/local/miniconda3/envs/<vent>/bin
Environment="VIRTUAL_ENV=/usr/local/miniconda3/envs/<vent>"
Environment="PATH=$VIRTUAL_ENV/bin:$PATH"
ExecStartPre=bash /root/.bashrc
ExecStart=/usr/local/miniconda3/envs/<vent>/bin/python -m http.server 8000 --bind <you_ip_adress> --directory /<path>/<to my http root>
Restart=always

[Install]
WantedBy=multi-user.target

I also call my ~/.bashrc where I call conda activate < < vent > >, but that does not seem working well as I see command not found in service status though it does not prevent from service starting.

If you know the way to issue conda activate < < vent > > just before a service startup - please, let me know, I appreciate it.

Thank you.

@ricferr

This comment has been minimized.

Copy link
Owner Author

@ricferr ricferr commented Mar 28, 2020

@dwesolowski

This comment has been minimized.

Copy link

@dwesolowski dwesolowski commented Mar 28, 2020

I use the below to run a python discord bot script with virtualenv

[Unit]
Description=Embed Bot
After=multi-user.target

[Service]
User=discord
Group=discord
Type=idle
WorkingDirectory=/opt/discord/embeds/
ExecStart=/opt/discord/embeds/bot-env/bin/python3 start.py
Restart=on-failure

[Install]
WantedBy=multi-user.target

start.py is the bot itself.

/opt/discord/embeds - root with the bot script
/opt/discord/embeds/bot-env - virtualenv

@641i130

This comment has been minimized.

Copy link

@641i130 641i130 commented May 21, 2021

Thank you this helped me solve an issue I was having!

@load-net

This comment has been minimized.

Copy link

@load-net load-net commented Jun 15, 2021

[Unit]
Description="run-script"

[Service]
User=root
Group=root
WorkingDirectory=/usr/src/python3.9-venv/
Environment=PATH=/usr/src/python3.9-venv/bin
ExecStart=/bin/sh -c 'cd /usr/src/python3.9-venv/ && source /usr/src/python3.9-venv/bin/activate && python3.9 /usr/src/python3.9-venv/welcomer.py'
Restart=on-failure

[Install]
WantedBy=multi-user.target

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