Learn to manage Linux startup processes and services using systemd, including handling service dependencies, restarts, and configurations.
In this article, you’ll learn how to manage Linux startup processes and services effectively. We will discuss how systemd—the init system—handles the startup sequence, restarts failed applications, and orchestrates service dependencies.When Linux boots, several critical applications launch automatically in a defined order. For instance, if “app2” depends on “app1”, then app1 starts before app2. This ordered process happens seamlessly behind the scenes. Furthermore, if a vital application crashes, systemd automatically restarts it to maintain system reliability.
The core mechanism responsible for this operation is the init system (short for initialization system). It reads configuration files called “units” that provide detailed instructions on how to:
Start the system and individual services.
Handle unexpected crashes.
Reload configurations or restart services.
Service units—files ending with a .service extension—specifically define how to manage applications. A typical service unit describes:
• The command to launch an application.
• The behavior when a program crashes.
• Commands to reload configurations or restart the service.To explore all available options for a service unit, run:
Copy
Ask AI
$ man systemd.service
This command opens the manual page that explains the configuration options for service units:
Copy
Ask AI
SYSTEMD.SERVICE(5) systemd.service SYSTEMD.SERVICE(5)NAME systemd.service - Service unit configurationSYNOPSIS service.serviceDESCRIPTION A unit configuration file whose name ends in ".service" encodes information about a process controlled and supervised by systemd. This man page lists the configuration options specific to this unit type. See systemd.unit(5) for the common options of all unit configuration files. The common configuration items are configured in the generic "[Unit]" and "[Install]" sections. The service specific configuration options are configured in the "[Service]" section.
For many servers, the SSH daemon is essential for remote connectivity. The SSH service unit guides systemd on how to start and maintain the daemon. To inspect this service file, use the command:
Managing service states with systemd involves starting, stopping, restarting, and reloading services. The following table summarizes common commands and their use cases:
Even after disabling a service, other components may inadvertently start it. Masking a service creates a symbolic link to /dev/null, ensuring the service can neither be started nor enabled.To mask a service like atd (which schedules tasks), execute:
Copy
Ask AI
$ sudo systemctl mask atd.service
Any attempt to start or enable the masked service will result in an error, as shown below:
Copy
Ask AI
$ sudo systemctl enable atd.serviceFailed to enable unit: Unit file /etc/systemd/system/atd.service is masked.$ sudo systemctl start atd.serviceFailed to start atd.service: Unit atd.service is masked.
To restore normal operations, simply unmask the service:
Service unit names can sometimes be confusing. For example, the Apache web server might be listed as “httpd.service” instead of “apache.service.” To list all service units, regardless of their state, run:
Copy
Ask AI
$ sudo systemctl list-units --type service --all
This command provides details like:
Copy
Ask AI
UNIT LOAD ACTIVE SUB DESCRIPTIONaccounts-daemon.service loaded active running Accounts Servicealsa-restore.service loaded inactive dead Save/Restore Sound Card Settingsalsa-state.service loaded active running Manage Sound Card State● apparmor.service not-found inactive dead apparmor.serviceatd.service loaded active running Job spooling toolsauditd.service loaded active running Security Auditing Serviceauth-rpcgss-module.service loaded inactive dead Kernel Module Supporting GSSAPI...
In this article, we covered how Linux manages startup processes and services using systemd. We explored service units and learned how to inspect, start, stop, reload, enable, disable, and mask services. With these tools and commands, you can manage critical applications effectively, ensuring your system remains stable.Now it’s time to put these concepts into practice in a lab environment to solidify your understanding.