UNIX Services
Check
systemctl status myservice
systemctl is-active myservice
Will return 0 if the service is runningsystemctl is-enabled myservice
Will return 0 if the service will automatically start at boot timesystemctl is-failed myservice
Possible return options are: active, failed, unknown, inactiveTo 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
Without an [Install] section you will be able to start manually, but you won't be able to 'enable' it for automatic startup.
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
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)
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.
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
- 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 dependenciessystemctl list-dependencies --before myservice
systemctl list-dependencies --after myservice
show
systemctl show myservice
Shows the low-level properties of your servicesystemctl show myservice -p After
Shows just the After property for your servicesystemd-sysv-generator
TODO
systemd-analyze
systemd-analyze
systemd-analyze blame
TODO
Bibliography
https://serverfault.com/questions/482730/systemd-dependencies-and-boot-orderhttps://serverfault.com/questions/736624/systemd-service-automatic-restart-after-startlimitinterval/962338
https://www.zdnet.com/article/nasty-linux-systemd-root-level-security-bug-revealed-and-patched/
systemd-analyzehttps://www.man7.org/linux/man-pages/man1/systemd-analyze.1.html
MySQLRouterhttps://support.oracle.comHow to Make MySQL Router Automatically Restart on Systemd (Doc ID 2684939.1)