SystemD and JournalD services

SystemD

Preliminaries

This note documents how to use the SystemD service manager to enable unattended background execution of the server software, and how to use the JournalD service to view and manage the server's logged output.

The Linux operating system uses SystemD to configure and manage background executables in a standard way. It uses "unit" files to configure the process user, process group, standard output destination, and most importantly, the command to start the server.

Unit file

The Service Manager unit file should be placed in /usr/lib/systemd/system/rwserve.service. The code below can be used as a template for a typical setup. Here are some things of note:

  • The "unit" name used in systemctl and journalctl commands is rwserve.
  • The Node.js software is installed by default to /usr/bin/node.
  • The main entry point to the Read Write Tools rwserve source code is installed by default to /usr/lib/node_modules/rwserve/dist/main.js.
  • The configuration file should be created and edited in the standard location, which for the DNS hostname example.com would be /etc/rwserve/example.com.conf.
  • The system user running the process is rwserve, created with the command "useradd --system --user-group rwserve".
  • The WorkingDirectory is not used by the server software but is still required by some Linux distros. The path pointed to must be accessible by the rwserve user, so in this example be sure to change the owner using "chown rwserve:rwserve /srv".
  • Standard log output is sent to the journal service.
[Unit]
Description=Read Write Tools rwserve

[Service]
ExecStart=/usr/bin/node /usr/lib/node_modules/rwserve/dist/main.js /etc/rwserve/example.com.conf
Restart=always
User=rwserve
Group=rwserve
WorkingDirectory=/srv
StandardOutput=journal
StandardError=null

[Install]
WantedBy=multi-user.target

SystemD commands

With the rwserve unit file properly edited and in place, the service can be instructed to start automatically each time the computer is booted. Use the enable command like this:

systemctl daemon-reload
systemctl enable rwserve

Three basic commands can be issued to the service to start execution, obtain its current status, and stop execution.

systemctl start rwserve
systemctl status rwserve
systemctl stop rwserve

JournalD commands

Each time the service is started, information is logged to the process's standard output, which is routed by the unit file to the system journal; also, during active operation, each request, process, and response is logged to the system journal as well. The command line utility journalctl is used to filter and display these log messages.

The system journal contains messages from many software components, so in order to limit the output to just the rwserve messages, the ‑‑unit or -u option should be used.

journalctl --unit rwserve

Message are shown in timestamp order, from oldest to most recent. Oftentimes it is more desirable to see the tail end of the messages. Use the -n to display the most recent N messages. To monitor the messages in real time, use the ‑‑follow or -f option.

journalctl --unit rwserve -n 100
journalctl --unit rwserve --follow

The system journal does not automatically rotate or purge messages and will continue to capture and store messages for all device units until all disk space is used up. This is annoying, but not cataclysmic, as the operating system and services will continue to run, albeit without the ability to create new files or log messages. To see how much space is being used by the journal use the ‑‑disk-space option. It is a good practice to periodically purge old messages using the journal's ‑‑vacuum-size option.

journalctl --disk-usage
journalctl --vacuum-size=500M

SystemD and JournalD services