Back to articles

Working with Services - Ubuntu

Published: 2025-01-29

I always forget how to create custom services and even how to start/restart etc.. since I do it so rarely.

Instead of scouring my command history or re-googling I’ll put my reminders below.

Common Commands

Did you make a service but you forgot what you called it? List all your services:

sudo systemctl list-units --type=service --all

Check status of your service:

sudo systemctl status yourapp.service

Restart your service:

sudo systemctl restart yourapp.service

Check logs of your service:

journalctl -u yourapp.service

Creating your own service

Say you’ve got a script that you want to run as a service instead of via cron or manually, you can set it up as a service instead and control it as such. So for example, if it ever dies it will come back on it’s own or a multitude of other ‘service-y’ things I’m not aware of. (I just wanted the resilience/self-healing aspect of a service, personally)

  1. Create the Service file (nano is the example here but use vi or whatever you like):
sudo nano /etc/systemd/system/wheatley-webapp.service
  1. Add the following configuration to the new .service file. Here’s an example:
[Unit]
Description=Start Wheatley Web App
After=network.target

[Service]
Type=simple
WorkingDirectory=/home/stavros/github/Multi-AI-Web-Stream
ExecStart=/home/stavros/wheatles/bin/python webapp.py
StandardOutput=append:/tmp/wheatleyWeb.log
StandardError=append:/tmp/wheatleyWeb.log
Restart=always
User=stavros

[Install]
WantedBy=multi-user.target

Exec start being what you want to execute. Restart lets you control if it will restart aftering dying or stopping

  1. Now with our service setup, we need to reload the systemd so that it picks up the changes:
sudo systemctl daemon-reload
  1. You can now start your service:
sudo systemctl start wheatley-webapp.service
  1. if you want it starting on boot:
sudo systemctl enable wheatley-webapp.service

You can check service status or logs via the commands at the beginning of this article.

Now you have a script that will run at boot and restart if it dies.

Congrats 🍻