How to Manage Services With Systemd
In the last few years, most Linux distributions have changed the way they start up. Previously, the UNIX style sysvinit ruled the roost aside from some outliers, such as Ubuntu which made use of Upstart for their operating systems. Recently, many Linux distributions have moved over to the new systemd process for managing the system startup and shutdown.
How to Work with Systemd for Managing Services on a Server
To start with, for those who are unfamiliar, we’ll have a quick look at how a Linux system starts up. For this the BIOS will first load the boot loader, which on most modern Linux systems is GRUB 2. The boot loader’s job is to find and start the Linux Kernel which acts as the interface between your software and the hardware on the computer. After it has started, the Kernel then looks for and starts the init system which is then responsible for initializing the various services and software on the system.
When working with the sysvinit system, you are probably familiar with using the scripts in /etc/init.d for starting and stopping services in conjunction with the service command. Systemd replaces this entirely and has its own command and syntax for starting and stopping services. For the majority of the work you’ll be doing with systemd, you’ll be using the systemctl command. So let’s use some examples to explain how systemctl works. Specifically, we are going to use the Apache2 service, but you can obviously replace Apache2 with the name of the service that you wish to manage. Note here that systemctl needs to be called as the root user or using sudo from a user account with the appropriate sudo permissions.
systemctl start apache2 – Starting with a simple one here, the above command starts the Apache2 service.
systemctl stop apache2 – As above nice and simple this stops the running Apache2 service.
systemctl enable apache2 – This command enables the Apache2 service to start automatically at bootup.
systemctl disable apache2 – Much like before this command disables the Apache2 service from starting automatically when the system boots.
systemctl status – This command shows you an overall status of the system including a process tree showing the services started by systemd.
systemctl status apache2 – This command would show you more detailed information about the Apache2 status including whether it is running or not as well as some truncated log file output from the last time it was started. This can be handy to see the reason why a service may have failed to start
systemctl status -l apache2 – This command provides pretty much the same output as above but also provides the full log output to help diagnose problems.
systemctl reload apache2 – This command would reload the configuration files for Apache2 without restarting it. Note that not all services are able to reload their configuration without restarting so this may not be an option for some services.
systemctl restart apache2 – This command would stop Apache2 and then start it again afterwards.
systemctl mask apache2 – The mask option is used to inform systemctl that you should not be able to start the service. So even attempts to manually start it will be blocked.
systemctl unmask apache2 – As you can probably guess, the unmask option reverses the function of the mask option.
systemctl list-unit-files – This command lists any unit files that systemd can find. Unit files are the instruction files that tell systemd what it needs to do in order to start/stop a service. These files are listed along with their state: “enabled” for services that will be started at boot, “disabled” for services that won’t, “masked” for services you have masked and “static” for files that systemd is unable to enable. This is handy for finding out the correct name for a service in order to manage it.
systemctl list-units – This is a companion to the previous command in that it lists any loaded units and a little bit of information about them.
systemctl poweroff – This command shuts down the computer.
systemctl reboot – This command reboots the computer.
That ends our whistle-stop tour of the various functions of systemd’s systemctl command and how you would use it to manage services running on your system. As with most commands, there’s more information to be found on the systemctl man page than we’ve included here. So, it’s worth checking it out to see what else you can do.