Managing Services with systemctl
The systemctl command is the main utility for managing services on systems using systemd. You can start, stop, restart, reload, enable, disable, and check the status of any service. Previously, we demonstrated listing and changing the system’s default target. Now, we will use Docker as an example service.Common Commands
Below are some frequently used commands:-
To start a service:
-
To stop a service:
-
To restart a service (this stops and then starts the service again):
-
To reload a service without interrupting its normal functionality:
-
To enable a service so that it starts automatically at boot:
-
To disable a service (preventing it from starting automatically at boot):
-
To check the status of a service:
systemctl status docker should indicate whether the service is running as expected. For example, an “active (running)” state confirms success. Below is a sample output:
In addition to the “active” state, be aware of:
- Inactive (dead): The service is stopped.
- Activating/Deactivating: Transitory states while a service is starting or stopping.
- Failed: Indicates an error, usually due to issues with the service’s command or configuration.
Reloading Systemd Configuration
Whenever you modify a unit file, reload the system manager configuration to apply changes:systemctl edit command. For example, to fully replace the configuration for the Project Mercury service:
Working with System Targets
Systemd targets (similar to runlevels) define the state of the machine. You can view or change the default target withsystemctl.
-
To view the current default target:
-
To change the default target (for example, to multi-user mode):
systemctl list-units without the --all flag.
Querying Logs with journalctl
The journalctl command is an essential troubleshooting tool for querying the systemd journal logs. By default, running journalctl displays all log entries from the oldest to the newest.-
To view logs for a specific unit, such as Docker:
Using
journalctl can help diagnose configuration issues or failed services by providing detailed log entries.