This post is intended to give you the basics of using systemd to start and stop services, adjust how existing services boot, how to override system services, and how to create new services. systemd is certainly complex and powerful, but the above probably describes 90% of what an admin is looking to do.
systemctl supersedes service, example:
systemctl restart nginx vs service nginx restart
Note: on CentOS7, you can still type ‘service nginx restart’ and it will map to ‘systemctl restart nginx’
In systemd, all the pieces are called ‘units’: services, mount points, devices, sockets are all considered units. Everything referenced here is referring to services – I did not have to get into any of the other unit types.
Basic commands:
systemctl – lists all units
systemctl status – detailed list of units’ status
systemctl show nginx – show the unit’s config details
systemctl start nginx
systemctl stop nginx
Other commands: restart, reload, status, is-enabled, enable, disable, mask, unmask, help
Good basic documentation: https://wiki.archlinux.org/index.php/systemd
Advanced commands: https://www.certdepot.net/rhel7-get-started-systemd/
Background: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units
Other systemd documentation: https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet
Things to know
– /etc/init.d still exists, but there’s little to nothing there. If you REALLY need to do something with SysV, you can still put scripts in /etc/init.d and symlink from rc2.d/rc3.d but it’s really going rogue
– /etc/init.d/ how-to: http://superuser.com/questions/278396/systemd-does-not-run-etc-rc-local
– The systemd system directory is: /usr/lib/systemd/system – this is where the system files are
– The “user-installed” or override directory is: /etc/systemd/system
CREATING A NEW CONFIG
We have a script that we want to execute when the server is booting up. We’re going to keep it in /etc/init.d/ even
though that directory isn’t really “processing” scripts from there.
– Create /etc/systemd/system/foobar.conf with the following text:
[Unit]
Description=Foobar
ConditionFileIsExecutable=/etc/init.d/foobar
[Service]
Type=simple
ExecStart=/etc/init.d/foobar start
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
– systemctl daemon-reload
Pay attention to the ‘Type’ of program: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
Good example of postfix.service file:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html#exam-Managing_Services_with_systemd-postfix_unit_file
Other unit config example: https://coreos.com/docs/launching-containers/launching/getting-started-with-systemd/
Documentation on unit config file: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
List of directives in unit config file: https://www.freedesktop.org/software/systemd/man/systemd.directives.html
ENV variable examples: https://coreos.com/os/docs/latest/using-environment-variables-in-systemd-units.html
More ENV examples: http://serverfault.com/questions/413397/how-to-set-environment-variable-in-systemd-service
NOTE: any time you make a chance to any file in either /usr/lib/systemd/system or /etc/systemd/system, you need to run: ‘systemctl daemon-reload’
TARGETS
– multi-user.target is run-level 3; I didn’t bother to learn how to do the other crap
– More: https://wiki.archlinux.org/index.php/systemd#Targets
OVERRIDING AN EXISTING SYSTEM UNIT IN /usr/lib/systemd/system
– run: systemctl edit —full [unit]
OR
– to override /usr/lib/systemd/system/[unit]
– create /etc/systemd/system/[unit]
– then run: systemctl reenable [unit]
ADDING/OVERRIDING A SNIPPET OF AN EXISTING SYSTEM UNIT
– run: systemctl edit [unit] (which creates /etc/systemd/system/[unit.d]/override.conf)
OR
– to add/edit a snippet for /usr/lib/systemd/system/[unit]
– create: /etc/systemd/system/[unit.d]
– put a .conf file with the unit config overrides (ex: /etc/systemd/system/[unit.d]/override.conf
– systemctl daemon-reload
CAVEAT: For ‘ExecStart’ changes, you need to clear the variable first in the snippet, then define it again. Example:
/etc/systemd/system/unit.d/override.conf
[Service]
ExecStart=
ExecStart=new command
DEBUGGING
– https://wiki.archlinux.org/index.php/systemd#Troubleshooting
– https://fedoraproject.org/wiki/How_to_debug_Systemd_problems