UNIX Services

Check

systemctl status myservice

systemctl is-active myservice

Will return 0 if the service is running

systemctl is-enabled myservice

Will return 0 if the service will automatically start at boot time

systemctl is-failed myservice

Possible return options are: active, failed, unknown, inactive

To view the logfile for the service...

journalctl -u myservice

To show the configuration file of a service...

systemctl cat myservice

Start

systemctl start myservice

Stop

systemctl stop myservice

Restart/Reload

Mask/UnMask

systemctl restart myservice

systemctl reload myservice

systemctl reload-or-restart myservice

Masking prevents a service being started (either manually or automatically)...

systemctl mask myservice

systemctl unmask myservice

Configuration

touch /etc/systemd/system/myservice.service

chmod 644 /etc/systemd/system/myservice.service

You may also find .service files in.../usr/lib/systemd/systemThis directory contains distribution level packages which will be overridden by any files in /etc/systemd/system. Always put your custom service files in /etc/systemd/system.
The unit file for your service should have [Unit] and [Service] sections.
Without an [Install] section you will be able to start manually, but you won't be able to 'enable' it for automatic startup.
[Unit]
Description - a short human readable name of the unit (service).
Documentation - a space seperated list of URIs of type http:, https:, file:, infor: or man:
Wants - lists units that systemd will attempt to start (if they are not already active) when this unit starts. If wanted units fail to start this unit will still start.
Requires - lists units that systemd will attempt to start (if they are not already active) when this unit starts. If required units fail to start this unit will also fail to start.
Requisite - if listed units are not already active then this unit will fail to start.
BindsTo - like Requires but if the bound to unit becomes inactive then this unit will stop.
[Install]
WantedBy - has the same effect as putting a Wants entry for this unit in the Unit section of the named unit's unit file. It does this via symbolic links (created when you enable) rather than updating the target unit file itself. The symbolic link is removed when you disable.
RequiredBy -similar to WantedBy but has the same affect as a Requires entry for this unit in the Unit section of the named unit's unit file.

For MySQL...

Example taken from MySQL documentation...[Unit]Description=MySQL ServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target
[Service]User=mysqlGroup=mysqlType=notifyTimeoutSec=0ExecStart=/mysql/current/bin/mysqld --defaults-file=/mysql/my.cnf $MYSQLD_OPTS EnvironmentFile=-/etc/sysconfig/mysqlLimitNOFILE = 10000Restart=on-failureRestartPreventExitStatus=1Environment=MYSQLD_PARENT_PID=1PrivateTmp=false
[Install]WantedBy=multi-user.target

For MySQL Router...

From Oracle Support Note 2684939.1[Unit]Description=MySQL RouterAfter=syslog.targetAfter=network.target
[Service]Type=simpleUser=mysqlrouterGroup=mysqlrouterPIDFile=/var/run/mysqlrouter/mysqlrouter.pidExecStart=/usr/bin/mysqlrouter -c /etc/mysqlrouter/mysqlrouter.confRestart=on-failurePrivateTmp=true
[Install]WantedBy=multi-user.target 
[Service]
Type - simple / exec / forking / oneshot / dbus / notify / idle - if in doubt, use simple. notify expects the service to send a notification message (via sd_notify or equivalent) when it has finished starting up.
User - sets the user that the processes are executed as.
Group - sets the group that the processes are executed as.
TimeoutSec - configures both TimeoutStartSec and TimeoutStopSec  to the specified value.
TimeoutStartSec - configures the time to wait for startup. Pass "infinity" (or "0" to disable timeout logic. Services of type "Notify" are able to send "EXTEND_TIMEOUT_USEC=" calls to extend startup beyond TimeoutStartSec.
TimeoutStopSec - configures the time to wait for each ExecStop command.  Also configures time to wait for service itself to stop. Pass "infinity" (or "0") to disable timeout logic. Services of type "Notify" are able to send "EXTEND_TIMEOUT_USEC=" calls to extend startup beyond TimeoutStopSec.
ExecStart - commands, with their arguments, that are executed when the service is started.
EnvironmentFile - specifies a file containing variable definitions.
Note that, although taken directly from the MySQL installation guide, the value "-/etc/sysconfig/mysql" is invalid due to the leading "-" (and on my test system, this file doesn't exist anyway) - the comment reads "Use this to switch malloc implementation", which may warrant further investigation (TODO)
Environment - variable definitions. E.g. for mysql setting MYSQLD_PARENT_PID to 1 is required for MySQL restart.
Restart - no (default) / always /
on-success / on-failure / on-abnormal / on-abort / on-watchdog. If you want your service to restart if it crashes but not if it cleanly exits, then use on-failure.
RestartPreventExitStatus - contains list of exit codes or signals that will not cause a restart even if they meet the criteria for the Restart setting.
LimitNOFILE - Sets open_files_limit (equivalent to ulimit -n)
PrivateTmp - If true, sharing between processes via /tmp/ or /var/tmp/ impossible and all temporary files created by a service in /tmp or /var/tmp will be removed after the service is stopped. 
PIDFile - Takes a path referring to the PID file of the service. Usage of this option is recommended for services where Type= is set to forking.

Automatically Start at Boot

systemctl enable myservice

systemctl disable myservice

Examples

Diagnostics

systemctl status service

journalctl -xe

list-units

To list all active units (services) that systemctl knows about...

systemctl list-units

To list only MySQL related active units...

systemctl list-units | grep mysql


mysql.mount                                                                              loaded active mounted   /mysqlmysqld.service                                                                           loaded active running   MySQL Servermysqlrouter.service                                                                      loaded active running   MySQL Router

To list all units that systemd has tried to load regardless of current state...

systemctl list-units --all

systemctl list-units --all | grep mysql

To list only inactive units...

systemctl list-units --all --state=inactive

list-unit-files

To see every available unit file within the systemd paths, including those that systemd has not attempted to load... 

systemctl list-unit-files

systemctl list-unit-files | grep mysql


mysqld.service                                enabled mysqlrouter.service                           enabled 
STATE returned will be one of...
  • enabled - will auto start
  • disabled - won't auto start
  • static - cannot be enabled (but could perform a one-off action or could be used as a dependency of another unit)
  • masked - cannot be started (service is linked to /dev/null).

list-dependencies

systemctl list-dependencies myservice

Outputs a dependency tree of all units that are either required or wanted by your service.

systemctl list-dependencies --all myservice

Recursively lists all dependencies

systemctl list-dependencies --before myservice

systemctl list-dependencies --after myservice

show

systemctl show myservice

Shows the low-level properties of your service

systemctl show myservice -p After

Shows just the After property for your service

systemd-sysv-generator

TODO

systemd-analyze

systemd-analyze

systemd-analyze blame

TODO

Bibliography