This article explains systemd, the Linux init system, focusing on service management using systemctl. It covers starting, stopping, enabling, disabling, and troubleshooting services, including log analysis via journalctl. The article also details c
Systemd is the init system used by most modern Linux distributions. It manages the starting, stopping, and overall lifecycle of system services. Unlike older init systems like SysVinit, systemd offers a more sophisticated and robust approach, leveraging features like dependency management and parallel service startup. Managing services with systemd involves using its command-line tools, primarily systemctl
. Understanding the structure of systemd units (files describing services, targets, and other system components) is crucial for effective management. These units are typically located in /etc/systemd/system/
and other related directories. The units themselves are usually written in a simple configuration language, making them relatively easy to understand and modify. Finally, systemd provides a robust logging mechanism, making troubleshooting much simpler than with older init systems.
The primary command for interacting with systemd is systemctl
. Here are some common commands for managing services:
systemctl start <service_name></service_name>
. For example, to start the SSH service, you would use systemctl start ssh
. This command initiates the service if it's not already running.systemctl stop <service_name></service_name>
. This command gracefully stops the specified service.systemctl restart <service_name></service_name>
. This command stops and then starts the service. Useful for applying configuration changes or recovering from minor issues.systemctl enable <service_name></service_name>
. This command ensures the service starts automatically at boot time. Systemd creates symbolic links to place the service unit in the appropriate runlevel directories.systemctl disable <service_name></service_name>
. This command removes the symbolic links, preventing the service from starting automatically on boot.systemctl status <service_name></service_name>
. This command displays the current status of the service, including its PID (Process ID), active state, and any recent logs. It provides detailed information about the service's health.systemctl list-units --type=service
. This command lists all currently running services. You can add --state=running
to filter for only running services.When a systemd service fails, several tools help in troubleshooting:
systemctl status <service_name></service_name>
: As mentioned earlier, this command is crucial. It provides information about the service's state, including error messages if any occurred during startup or runtime. Pay close attention to the "FAILED" or "inactive (dead)" status and the subsequent error messages.Journalctl: The journalctl
command is the primary tool for viewing systemd logs. It allows you to filter logs based on service name, time, and other criteria. For example:
journalctl -u <service_name></service_name>
: Shows logs specifically for the specified service.journalctl -u <service_name> -xe</service_name>
: Shows the most recent error messages for the service.journalctl -u <service_name> -f</service_name>
: Follows the log output in real-time. Useful for observing the service's behavior during troubleshooting./etc/systemd/system/
) for any syntax errors or incorrect configurations.Creating a custom systemd service involves creating a unit file describing the service. These files typically have a .service
extension. Here's a basic example of a service file:
[Unit] Description=My Custom Service After=network.target [Service] Type=simple User=myuser Group=mygroup ExecStart=/path/to/my/script.sh Restart=always RestartSec=5 [Install] WantedBy=multi-user.target
[Unit]
section: Contains metadata about the service, including its description and dependencies.[Service]
section: Defines how the service is run, including the command to execute (ExecStart
), the user and group to run it as, and restart policies.[Install]
section: Specifies where the service should be placed in the systemd hierarchy.After creating this file (e.g., /etc/systemd/system/mycustomservice.service
), you need to:
systemctl daemon-reload
systemctl enable mycustomservice
systemctl start mycustomservice
systemctl status mycustomservice
Remember to replace placeholders like /path/to/my/script.sh
, myuser
, and mygroup
with your actual values. Properly setting the user and group is essential for security. The Restart=always
and RestartSec=5
options ensure the service restarts automatically if it crashes, waiting 5 seconds before attempting a restart. Always test your custom service thoroughly before deploying it to a production environment.
The above is the detailed content of How do I manage system services with systemd in Linux?. For more information, please follow other related articles on the PHP Chinese website!