Managing Services (systemctl, service)
1. Introduction to Service Management
In Linux, services (also known as daemons) are programs that run in the background, performing specific tasks like managing network connections, running web servers, or handling database operations.
System administrators can manage these services by using init systems like Systemd (which has replaced older systems like SysVinit) and commands like systemctl
and service
.
2. Managing Services with systemctl (Systemd)
The systemctl
command is the primary way to manage services in Systemd.
It allows you to control, inspect, and manage system services (units).
a. Starting a Service
The systemctl start
command is used to start a service on-demand.
Command:
sudo systemctl start apache2
This command will start the Apache2 service, allowing it to serve web pages. The service will run until it is manually stopped or the system is rebooted.
Note: Starting a service only affects the current session. To make the service start automatically at boot, you need to enable it (covered later).
b. Stopping a Service
The systemctl
stop command is used to stop a running service.
sudo systemctl stop apache2
This command stops the Apache2 service, terminating all its running processes. If the service was handling requests, they will be interrupted.
c. Enabling a Service to Start Automatically on Boot
To ensure that a service starts automatically when the system boots, you use systemctl enable.
sudo systemctl enable apache2
This command creates the necessary symbolic links in the appropriate directories to ensure that Apache2 is automatically started during the system boot process.
Note: Enabling a service doesn’t start it immediately; it just configures it to start when the system restarts.
d. Disabling a Service from Starting on Boot
If you want to stop a service from automatically starting on boot, use systemctl disable.
sudo systemctl disable apache2
This removes the symbolic links that systemctl enable created, preventing Apache2 from starting at boot. However, this does not stop the service if it’s already running.
3. Checking the Status of a Service
To check the status of a service (whether it’s active, inactive, or failed), use the systemctl
status command.
systemctl status apache2
This command gives detailed information about the Apache2 service, including its current status (active, inactive, or failed), and displays logs or errors related to its last run.
You can also view the last few lines of logs related to that service.
4. Restarting and Reloading Services
Sometimes, you need to restart a service to apply configuration changes or to recover from errors.
a. Restarting a Service
sudo systemctl restart apache2
Restarts the Apache2 service. This is useful when you make changes to the service configuration files and need to apply them without rebooting the system.
b. Reloading a Service
Some services support reloading configuration files without shutting down completely.
sudo systemctl reload apache2
This reloads the Apache2 configuration, applying changes without terminating the service or interrupting its current operation.
5. Managing Services with service
Command (For Legacy Systems)
The service command is part of the SysVinit system and is mostly used in legacy distributions or when Systemd is not available. However, it is still available on many systems as a compatibility layer.
a. Starting a Service with service
sudo service apache2 start
This command starts the Apache2 service using SysVinit scripts. While it’s less common now, it’s still used for backward compatibility on some Linux distributions.
b. Stopping a Service with service
sudo service apache2 stop
This command stops the Apache2 service, similar to systemctl
stop in newer distributions.
c. Restarting a Service with service
sudo service apache2 restart
This restarts the Apache2 service, allowing changes to its configuration to take effect.
d. Checking the Status of a Service with service
sudo service apache2 status
This checks the current status of the Apache2 service (running or not), along with some basic details like whether the process is active.
6. Comparing systemctl
and service
Feature | systemctl (Systemd) | service (SysVinit) |
---|---|---|
Start a Service | systemctl start <service> | service <service> start |
Stop a Service | systemctl stop <service> | service <service> stop |
Enable a Service | systemctl enable <service> | Not available in service |
Disable a Service | systemctl disable <service> | Not available in service |
Restart a Service | systemctl restart <service> | service <service> restart |
Status of Service | systemctl status <service> | service <service> status |
Key Difference:
systemctl
is used in modern Linux distributions with Systemd. On the other hand, service
is for older distributions using SysVinit or Upstart. However, many modern distributions still maintain the service
command as a compatibility feature.
7. Key Takeaways:
- How to start, stop, enable, and disable services using systemctl.
- The basic usage of the legacy service command.
- Differences between the systemctl (modern) and service (legacy) commands for managing services.