Service Management in Linux
The Essential Guide to Linux Service Management
Introduction
Welcome everyone! ๐๐ป
In the previous part of this series, you learned how to manage packages on different Linux systems. In this part, we'll dive into the services world and how to manage them.
So without further ado, let's get started! ๐
Services
Services are the workhorses of a Linux system. They are the programs that run in the background, providing essential functionality. They are also known as daemons because they are typically invisible to users, running in the background without any user interaction.
For example, the ssh service that allows you to log in to a remote system over the Internet.
Systemd
If you remember from the first part when the kernel finishes its initialization, it creates a process called init. The init process is responsible for starting and managing all of the system's services and daemons.
Systemd is an implementation of init that is used by the majority of Linux distributions. It is the standard way to manage services in Linux. It provides a simple and consistent way to manage services, regardless of the distribution you are using. You can also use it to configure the dependencies between services.
Unit & Unit files
Units are the entities that systemd manages. They can be services, sockets, devices, mount points, swap files, startup targets, and more. You can learn more about systemd units in the systemd.unit
man page.
Unit files are the configuration files for systemd units. They are typically stored in the /usr/lib/systemd/system
directory, but they can also be stored in other directories.
When you install a package, it may create unit files in the /usr/lib/systemd/system
directory. However, you should not modify these files, as they may be overwritten by the package manager. If you want to create or modify a unit file, you should create it in the /etc/systemd/system
directory. This directory is used for user-defined unit files. Unit files are named according to the type of unit they define. For example, a service unit file would be named myservice.service
.
In this tutorial, we will focus on service units. A service unit defines a process that systemd should start and manage.
Service Management
Systemd is a suite of system and service management tools for Linux. It is not a single daemon, but a collection of programs, daemons, libraries, and kernel components.
The systemctl command is used to control the state of systemd. It can be used to start, stop, restart, and check the status of units.
Let's see it in action!
Check service status
To check the status of a service use the systemctl status <service>
command. This command will show you the current status of the service, including whether it is running, stopped, or disabled. It will also show you the reason for the service's current status.
In this case systemctl shows us that the apache service is inactive (dead) and disabled. This means that the Apache web server is not running and it will not start automatically when the system boots up.
Start service
To start a service, use the systemctl start <service>
command as a root or a user with sudo privileges. This command will start the service immediately.
You can also use the systemctl status <service>
command to check the status of a service.
In this case the output shows that the apache service is running and has been started 38 seconds ago. It also shows the process ID (PID), the amount of memory, and the amount of CPU time it is using.
Stop service
To stop a service, use the systemctl stop <service>
command as a root user or a user with sudo privileges. This will stop the service immediately.
Here the outputs shows us that the service is currently inactive (dead) and some recent logs for the service. These logs can be helpful for debugging problems with the service.
Restart service
To restart a service after changing its configuration, use the systemctl restart <service>
command as a root user or a user with sudo privileges. This will start the service immediately if it is not already running.
In this case apache service was not running that's why systemctl restart
command started it.
Enable service autostart
To enable a service to start automatically at startup, use the systemctl enable <service>
command as a root user or user with sudo privileges. This will only enable the service, it will not start it immediately.
Here the apache service is enabled to start at startup.
To enable and start a service immediately use the systemctl enable --now <service>
command.
As you can see above the service is enabled as well as active (I disabled it for this example).
Disable service autostart
To disable a service from starting automatically when system boots up, use the systemctl disable
command as a root user or user with sudo privileges. You can verify this by rebooting your system and then checking the status of the service
As you can see above that the apache service is disabled to start when the system boots up.
To disable and stop a service immediately use the systemctl disable --now <service>
command.
This time the service is immediately stopped.
Reload systemd daemon
To make sure that systemd is using the latest configuration, run the systemctl daemon-reload
command as root or with sudo privileges.
This command will reload all unit files, which are the configuration files for services and other processes.
If you have made any changes to systemd configuration files, you need to run systemctl daemon-reload
before the changes will take effect.
Reboot system
To reboot a system, you can use the systemctl reboot
command. This will gracefully shut down all running processes and then restart the system.
There is also a shorter command called reboot
, which is a soft link to systemctl reboot
. You can check this by running the ls -l $(which reboot)
command.
That's all for this part!
I hope you found this article informative and helpful. If you have any feedback, please share it in the comments below. Thank you for reading!
Stay tuned for the next part of the Master Linux series!