Setting up a systemd Service under Ubuntu

Posted on Sat 11 November 2017 in Sysadmin

Starting with 15.04, Ubuntu uses systemd instead of Upstart as the default system and service manager. This page offers a good comparison between the two systems.

If you want to add a new systemd service (e.g. for running a Java application everytime your machine boots), create the following file under /etc/systemd/system/myservice.service:

[Unit]
Description=My Service Description
After=syslog.target

[Service]
WorkingDirectory=/path/to/workdir
SyslogIdentifier=MyService
EnvironmentFile=/path/to/workdir/myservice.environment
ExecStart=/bin/bash -c "java -jar /path/to/jar/MyService.jar"
User=uwe
# see https://stegard.net/2016/08/gracefully-killing-a-java-process-managed-by-systemd/
SuccessExitStatus=143
Type=simple

[Install]
WantedBy=multi-user.target

After creating or modifying a unit file, you should reload the systemd process itself to pick up your changes:

sudo systemctl daemon-reload

To enable a service to start automatically at boot, type:

sudo systemctl enable unsereernte.service

You can start the service by typing:

sudo systemctl start unsereernte.service

Aside from start, you can stop, restart, or reload the service.

A systemd component called journald collects and manages journal entries from all parts of the system. This is basically log information from applications and the kernel.

To see all log entries, starting at the oldest entry, type:

journalctl

A good tutorial on systemd services can be found here.