Managing systemd Services: Install, Start, Stop, Pause, and Resume
systemd
is a powerful system and service manager for Linux, widely used across major distributions like Ubuntu, Fedora, and Arch Linux. It provides a unified way to manage system processes, services, and resources. This guide covers the essentials of installing, starting, stopping, pausing, and resuming services with
systemd.
1. Installing Systemd Services
In most Linux distributions,
systemd
is pre-installed. However, if you're creating a new service or installing software that includes a
systemd
service, you may need to manually configure or install the service file.
Creating a Systemd Service File
To create a custom service file for
systemd, follow these steps:
-
Create a new service file
in the
/etc/systemd/system/directory:sudo nano /etc/systemd/system/myservice.service -
Define the service
by adding the following basic structure:
[Unit] Description=My Custom Service After=network.target [Service] ExecStart=/path/to/executable Restart=on-failure [Install] WantedBy=multi-user.target-
Description: A short description of the service.
-
After: Specifies dependencies, so the service starts after these dependencies are loaded.
-
ExecStart: The command to execute when the service starts.
-
Restart: Defines the service restart behavior (e.g., restart on failure).
-
WantedBy: Defines the target for the service (multi-user mode in this case).
-
Description: A short description of the service.
-
Reload
systemdto recognize the new service file:sudo systemctl daemon-reload -
Enable the service
to start automatically at boot:
sudo systemctl enable myservice
2. Starting and Stopping a Systemd Service
Once a service is installed and configured, you can start or stop it with
systemctl.
Start a Service
To start a
systemd
service:
sudo systemctl start myservice
-
Replace
myservicewith the actual service name. -
This command immediately runs the service specified in the
ExecStartdirective.
Stop a Service
To stop a
systemd
service:
sudo systemctl stop myservice
This halts the service without affecting its configuration or boot behavior.
Enable and Disable a Service
-
Enable
the service to start on boot:
sudo systemctl enable myservice -
Disable
the service to prevent it from starting on boot:
sudo systemctl disable myservice
3. Pausing and Resuming a Systemd Service
systemd
does not directly support pausing services, but you can simulate pausing by suspending the main process. Here's how:
Pausing a Service Using SIGSTOP
-
First,
identify the main process ID (PID)
for the service:
sudo systemctl show -p MainPID --value myservice -
Send the SIGSTOP signal
to pause the process:
sudo kill -SIGSTOPReplace
<PID>with the process ID obtained from the previous command. This halts the process without stopping the service entirely.
Resuming a Paused Service Using SIGCONT
To resume the paused service, send the SIGCONT signal:
sudo kill -SIGCONT
This resumes the process from where it was paused.
4. Checking the Status of a Service
To view the current status of a service, use:
sudo systemctl status myservice
This command provides details, including whether the service is active, inactive, failed, or paused (if manually paused with SIGSTOP).
5. Restarting a Systemd Service
You may want to restart a service to apply new configurations or to refresh its state. To restart a service:
sudo systemctl restart myservice
This command stops and then starts the service in one step.
6. Additional Systemd Commands
Here are some additional
systemd
commands for managing services:
-
Reload a Service Configuration: Reloads the configuration without fully restarting:
sudo systemctl reload myservice -
List All Services:
systemctl list-units --type=service -
View Logs for a Service:
systemdis integrated with thejournalctllogging system. To view logs for a service, use:sudo journalctl -u myservice
Summary
With
systemd, managing services becomes more standardized and flexible. This guide covered:
-
Installing and configuring
systemdservices -
Starting, stopping, enabling, and disabling services
-
Simulating pause and resume with SIGSTOP and SIGCONT signals
-
Checking status and viewing logs
Understanding
systemd
allows for efficient control of processes on Linux systems, whether for managing background services or building a reliable server environment.