Skip to main content
Now that your application is running as a service, let’s explore essential systemd tools that let you manage services, work with system targets, query logs, and gather overall system state information. In this guide, we focus on two primary utilities: systemctl and journalctl.

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:
The output of 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:
For editing unit files, you can use the systemctl edit command. For example, to fully replace the configuration for the Project Mercury service:
After saving your changes, systemd immediately applies them without requiring an additional daemon reload.

Working with System Targets

Systemd targets (similar to runlevels) define the state of the machine. You can view or change the default target with systemctl.
  • To view the current default target:
  • To change the default target (for example, to multi-user mode):
Additionally, list all units (both loaded and attempted) using:
A snippet of the output might appear as follows:
To view only active units, run 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:
A sample output may look like:
Using journalctl can help diagnose configuration issues or failed services by providing detailed log entries.
The image outlines systemd tools, highlighting "systemctl" for managing system states and "journalctl" for querying systemd journals.

Conclusion

With the commands and examples provided above, you now have a solid foundation in managing services with systemctl and troubleshooting them using journalctl. Bob’s working Project Mercury service on his laptop is just one example of how systemd can simplify system administration tasks. Practice these commands further by creating your own systemd service and applying these management and troubleshooting techniques. For additional resources, consider exploring:

Watch Video

Practice Lab