Controlling a Process with systemd

Create the Unit File

vim /etc/systemd/system/queue.service

The unit file must have three sections (headers denoted by square brackets): [Unit], [Service] and [Install].

Deciding on Service Type

Choose from the following: simple, forking, oneshot, dbus, notify, idle.

Prefer the simple type which handles programs that run in the foreground. Use the forking type for programs that fork themselves into the background.

Tell systemd what to do when your service exits (default is Restart=no); the choices are no (default), always, on-success, on-failure, on-abnormal, on-watchdog, on-abort. Delay restart with RestartSec=.

More Configuration

Common variables:

WorkingDirectory= (change directory before executing processes)
RootDirectory= (chroot)
User=
Group=
UMask=
Environment= (variables)

Variable docs

Example of a Common Basic Service Unit File

# /etc/systemd/system/queue.service
[Unit]
Description=Queue Handler
After=network.target

[Service]
Type=simple
Restart=always
ExecStart=php /var/www/deploy/rxspark/current/artisan queue:listen redis --queue=test

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable queue.service
Created symlink /etc/systemd/system/multi-user.target.wants/queue.service → /etc/systemd/system/queue.service.

View the Service's Output

systemctl enable queue.service

and

journalctl -xefu queue.service | less

Or if you fancy adding a log command to systemctl (shout out to the stack overflow user who proposed it; I did not save the link):

systemctl() {
    if [[ "${1-}" == "log" ]]; then
        /usr/bin/journalctl -u "${@:2}";
    else /usr/bin/systemctl "$@";
    fi   
}

Tags

 systemd  sysadmin  Linux