I have a number of bottle.py web services that I run on my servers. For the last few years, I’ve been using tmux to launch these services and then detaching them so they continue running after I logout of the SSH session. Recently, one of those servers had an issue causing the provider to require moving my VPS to another machine. So, when I went to use it, I got the 503 Service Unavailable message (that no one likes to see)!

Today, I finally got around to setting up systemd service files for these services. It only took about an hour (including troubleshooting things not working). Below is the shopping.service file (for a web based shopping list I wrote):

# Save this file in ~/.config/systemd/user/shopping.service
# then run:
# $ systemctl --user daemon-reload
# $ systemctl --user enable shopping.service
# Below will prevent the service stopping when we terminate the SSH session
# $ sudo loginctl enable-linger username

[Unit]
Description=Shopping List (bottle.py)
After=network.target

[Service]
Type=simple
WorkingDirectory=/home/simon/web/shopping
ExecStart=/usr/bin/python3 /home/simon/web/shopping/shopping.py
Restart=always

[Install]
WantedBy=default.target

Note: make sure to follow the comments. Otherwise, you’ll also have problems.

Once you’ve created and symlinked the file to ~/.config/systemd/user, you’ll be able to check all is good as you can for any system process.

simon@server:~/web/shopping [ssh] $ systemctl --user status shopping_list.service 
● shopping_list.service - Shopping List (bottle.py)
     Loaded: loaded (/home/simon/.config/systemd/user/shopping_list.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-03-25 20:26:29 GMT; 9min ago
   Main PID: 32632 (python3)
      Tasks: 11 (limit: 1129)
     Memory: 100.3M
        CPU: 7.471s
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/shopping_list.service
             └─32632 /usr/bin/python3 /home/simon/web/shopping/run_sl2.py

I’ll monitor this one service for a while, then I plan to use systemd service files for all my web services.