How to run python web server forever on Linux


To run nodejs webserver forever, you can use forever like:

forever start index.js

How to do this with python ?

There are many methods, i can show one of them:

Step 1:

vi /etc/systemd/system/my-python-daemon.service

content:

[Unit]
Description=My Python App

[Service]
Type=simple
WorkingDirectory=/root/photoshop/BASNet-http
ExecStart=/usr/bin/python3 main.py
Restart=always
# Restart service after 10 seconds if python service crashes
RestartSec=10

[Install]
WantedBy=multi-user.target

Step 2:

systemctl enable my-python-daemon
systemctl start my-python-daemon

Result:

[root@tutorialspots ~]# systemctl enable my-python-daemon
Created symlink from /etc/systemd/system/multi-user.target.wants/my-python-daemon.service to /etc/systemd/system/my-python-daemon.service.
[root@tutorialspots ~]# systemctl start my-python-daemon

Leave a Reply