Ubuntu System Services

This post about systemd has been published from a draft, so it’s not been proofed for publication. I find it useful, so I would like it readily available on-line. If you find anything wrong with it, please

Ubuntu, like many GNU/Linux distributions, uses systemd. systemd is a system used by the OS tor manage process, known as services. We can add our own services to systemd, and I’ll show how to do that in this post.

Services are useful, for example, if you start and run server. If we start that server as a user, the process running the server will be killed when the user logs out. If we want the server to run continuously – which is usually the case with servers – this is a problem.

We can be keep the service running by abandoning the process before logout, but it’s a hassle. In this set up, we would need to login, start the server, then abandoning processes. Instead, we can use systemd services and have the server started automatically (or on demand) and it runs in the background.

Systemd Unit Files

Our unit files go in the Systemd system directory.

/etc/systemd/system

Each service should have a name of the following form:

[name].service

Here’s an example of the format of the file.

[Unit]
Description=My Node.js Service

[Service]
WorkingDirectory=/home/jason/my-app
Restart=on-failure
ExecStart=/home/jason/my-app/dist/index.js

# Note: Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'.
# Note: when 'ExecStart' is used, 'Type' defaults to 'simple'.

Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV='production'
Environment=ORACLE_HOME=/usr/lib/oracle/18.3/client64
Environment=LD_LIBRARY_PATH=/usr/lib/oracle/18.3/client64/lib

[Install]
WantedBy=multi-user.target

For more information on Service files, see the man page.

man systemd.service

Update Systemd Services

 sudo systemctl daemon-reload 
 
sudo systemctl enable my-webapp.service

Created symlink /etc/systemd/system/multi-user.target.wants/event-tracker-api.service → /etc/systemd/system/event-tracker-api.service.

 
sudo systemctl start my-webapp
sudo systemctl stop my-app
sudo systemctl status my-webapp

Command:

sudo service [name] {start,stop,restart,status}

Leave a Reply

Your email address will not be published.