Skip to content

Instantly share code, notes, and snippets.

@thevickypedia
Last active March 23, 2024 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thevickypedia/6cab2deffc5b2b0b11a2ab65354ad852 to your computer and use it in GitHub Desktop.
Save thevickypedia/6cab2deffc5b2b0b11a2ab65354ad852 to your computer and use it in GitHub Desktop.
Run python script as a daemon service in Linux

Create a service

/etc/systemd/system/pyfilebrowser.service
Contents
[Unit]
Description=PyFilebrowser Service
After=network-online.target

[Service]
Type=simple
WorkingDirectory=/home/vicky/pyfilebrowser
ExecStart=/home/vicky/pyfilebrowser/venv/bin/python /home/vicky/pyfilebrowser/temp.py
User=vicky
Group=vicky
Environment="PATH=/home/vicky/pyfilebrowser/venv/bin:$PATH"
StandardOutput=append:/var/log/pyfilebrowser/output.log
StandardError=append:/var/log/pyfilebrowser/error.log

[Install]
WantedBy=multi-user.target

User and Group are not required if the permissions to the files are changed to www-data
Since that involves changing permissions even for modules within venv, it is simpler to run as user

Enable Service

sudo systemctl enable pyfilebrowser.service

Reload Daemon

sudo systemctl daemon-reload

Start Service

sudo systemctl start pyfilebrowser.service

[OR]

sudo service pyfilebrowser start

Service Status

sudo systemctl status pyfilebrowser.service

[OR]

sudo service pyfilebrowser status

Service Stop

sudo systemctl stop pyfilebrowser.service

[OR]

sudo service pyfilebrowser stop

Service Logs

journalctl -u pyfilebrowser.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment