Activates a service immediately:
systemctl start foo.serviceDeactivates a service immediately:
systemctl stop foo.serviceRestarts a service:
systemctl restart foo.serviceShows status of a service including whether it is running or not:
systemctl status foo.serviceEnables a service to be started on bootup:
systemctl enable foo.serviceDisables a service to not start during bootup:
systemctl disable foo.serviceCheck whether a service is already enabled or not:
0 indicates that it is enabled. 1 indicates that it is disabled
systemctl is-enabled foo.service; echo $?Change ad-hoc runlevel with systemctl isolate command:
Switch to another target (in this case multi-user/runlevel 3 in old SysV):
systemctl isolate multi-user.targetSwitch to graphical target (in this case graphical/runlevel 5 in old SysV):
systemctl isolate graphical.targetChange permanently change default.target:
Remove configured default target
rm /etc/systemd/system/default.targetCreate default.target as symbolic link to multi-user.target
ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.targetSet default target through systemctl
To find a [new target]:
- To get active targets,
systemctl --type=target list-units
- To get all targets, (active/inactive)
systemctl --type=target --all list-units
- To list available targets, (ls -l /usr/lib/systemd/system/*.target)
systemctl --type=target list-unit-files
systemctl set-default [new target]
List-units by pattern:
systemctl list-units *etwok*.service
Example content:
UNIT LOAD ACTIVE SUB DESCRIPTION
network.service loaded active exited LSB: Bring up/down networking
NetworkManager.service loaded active running Network Manager
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
2 loaded units listed. Pass --all to see loaded but inactive units, too.Display a content of unit file
systemctl cat network.serviceDisplayed content:
# /run/systemd/generator.late/network.service
# Automatically generated by systemd-sysv-generator
[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/network
Description=LSB: Bring up/down networking
Before=runlevel2.target runlevel3.target runlevel4.target runlevel5.target shutdown.target network-online.target network.target
After=iptables.service ip6tables.service NetworkManager-wait-online.service NetworkManager.service
Wants=network-online.target
Conflicts=shutdown.target
[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/network start
ExecStop=/etc/rc.d/init.d/network stop
ExecReload=/etc/rc.d/init.d/network reloadSuppress non zero exit code (service after stop will be displayed as inactive, not failed):
[Service]
SuccessExitStatus=143Link:
- [services-remain-in-failed-state-after-stopped-with-systemctl] (https://serverfault.com/questions/695849/services-remain-in-failed-state-after-stopped-with-systemctl)
How to put PID of service as variable to service systemd file:
ExecReload=/bin/kill -s HUP $MAINPIDLink
- you-should-be-using-pidfile-and-mainpid-instead-of-pkill-1935e4531931
- https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStop=
List all services with pattern
<pattern> - httpd e.g
systemctl list-units -t service --full| cut -d' ' -f1 | grep <pattern> | while read s; do systemctl status $s; done
Link
Reference links:
- [systemd.service man] (https://www.freedesktop.org/software/systemd/man/systemd.service.html)
- [Useful SystemD commands] (http://www.dynacont.net/documentation/linux/Useful\_SystemD\_commands/)
- [Archlinux systemd wiki] (https://wiki.archlinux.org/index.php/Systemd)
- [Red Hat RHEL 7 systemd documentation] (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/chap-Managing_Services_with_systemd.html)
- [difference-between-systemd-and-terminal-starting-program] (https://unix.stackexchange.com/questions/339638/difference-between-systemd-and-terminal-starting-program/339645#339645)
- [where-do-i-put-my-systemd-unit-file] (https://unix.stackexchange.com/questions/224992/where-do-i-put-my-systemd-unit-file)
- [trouble-creating-pid-file-in-systemd-service-script] (https://serverfault.com/questions/824543/trouble-creating-pid-file-in-systemd-service-script)
- [Fedora - Packaging:Systemd] (https://fedoraproject.org/wiki/Packaging:Systemd)
- [controlling-a-multi-service-application-with-systemd] (http://alesnosek.com/blog/2016/12/04/controlling-a-multi-service-application-with-systemd/)
- [rhel7-use-systemd-timers] (https://www.certdepot.net/rhel7-use-systemd-timers/)
- [how-to-remove-systemd-services] (https://superuser.com/questions/513159/how-to-remove-systemd-services)
- [systemd-containers-introduction-systemd-nspawn] (https://blog.selectel.com/systemd-containers-introduction-systemd-nspawn/)
- [archlinux systemd-nspawn] (https://wiki.archlinux.org/index.php/systemd-nspawn)
- [sd_notify python watchdog implementation] (https://gist.github.com/Spindel/1d07533ef94a4589d348)
- [sd_notify python service notify implemetation] (https://github.com/bb4242/sdnotify)
- [python sdnotify library] (https://pypi.org/project/sdnotify/)
- [creating-a-linux-service-with-systemd] (https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6)
- [systemd.io] (https://systemd.io/)
- systemd-ignores-return-code-while-starting-service
- how-do-i-override-or-configure-systemd-services
Pid Eins:
- [systemd-for-admins-I] (http://0pointer.net/blog/projects/systemd-for-admins-1.html)
- [systemd-for-admins-II] (http://0pointer.net/blog/projects/systemd-for-admins-2.html)
- [systemd-for-admins-II] (http://0pointer.net/blog/projects/systemd-for-admins-3.html)
- [systemd-for-admins-IV] (http://0pointer.net/blog/projects/systemd-for-admins-4.html)
- [systemd-for-admins-V] (http://0pointer.net/blog/projects/three-levels-of-off.html)
- [systemd-for-admins-VI] (http://0pointer.net/blog/projects/changing-roots.html)
- [systemd-for-admins-VII] (http://0pointer.net/blog/projects/blame-game.html)
- [systemd-for-admins-VIII] (http://0pointer.net/blog/projects/the-new-configuration-files.html)
- [systemd-for-admins-IX] (http://0pointer.net/blog/projects/on-etc-sysinit.html)
- [systemd-for-admins-X] (http://0pointer.net/blog/projects/instances.html)
- [systemd-for-admins-XI] (http://0pointer.net/blog/projects/inetd.html)
- [systemd-for-admins-XII] (http://0pointer.net/blog/projects/security.html)
- [systemd-for-admins-XIII] (http://0pointer.net/blog/projects/systemctl-journal.html)
- [systemd-for-admins-XIV] (http://0pointer.net/blog/projects/self-documented-boot.html)
- [systemd-for-admins-XV] (http://0pointer.net/blog/projects/watchdog.html)
- [systemd-for-admins-XVI] (http://0pointer.net/blog/projects/serial-console.html)
- [systemd-for-admins-XVII] (http://0pointer.net/blog/projects/journalctl.html)
- [systemd-for-admins-XVIII] (http://0pointer.net/blog/projects/resources.html)
- [systemd-for-admins-XIX] (http://0pointer.net/blog/projects/detect-virt.html)
- [systemd-for-admins-XX] (http://0pointer.net/blog/projects/socket-activated-containers.html)
Digital Ocean:
- [systemd-essentials-working-with-services-units-and-the-journal] (https://www.digitalocean.com/community/tutorials/systemd-essentials-working-with-services-units-and-the-journal)
- [How-to-use-systemctl-to-manage-systemd-services-and-units] (https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units)
- [Understanding-systemd-units-and-unit-files] https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files