Systemd initialization system allows you not only to start and stop services but also to check their status and show information about them. Use the systemctl utility with the status command to get information about the service. For example, if you want to get the state of the Nginx web server, run this command:
sudo systemctl status nginx
Usually, the utility prints information with pagination because data does not fit the terminal window. If you want to disable pagination, use the --no-pager option:
sudo systemctl status nginx --no-pager
Let's have a look at the information which is printed by this command:
- Loaded - the loaded value means that the unit file was loaded successfully. Here you can see other values, for example, masked, when the unit is masked or not-found when the requested unit does not exist. Also in this line, you can find the path to the unit file.
- Active - the current unit state and sub-state. If the unit is running you will see active (running) or inactive(dead) if nobody has started it since the last system boot. If something goes wrong, you will see the failed state.
- Docs - the name of man pages for the service.
- Process - service processes, their states, and exit codes.
- Main PID - identifier of the main service process.
- Tasks - number of tasks which are running for this service.
- Memory - reflects how much RAM is used.
- CPU - reflects the CPU usage.
If you want to get all possible values for the Loaded and Active fields, run this command:
systemctl --state help
After these information fields, you can see the service log. By default, it prints the last ten lines from the main service output. If you want to get more lines, use the --lines option. For example, use this command if you want to get the last 50 lines:
sudo systemctl status --lines=50 avahi-daemon
In rare cases, for example in no-pager mode, log lines may be ellipsized to fit the screen. You can use the -l or --full options to avoid this:
sudo systemctl -l status avahi-daemon --no-pager
If you want to get all available logs, you should use the journalctl command with the -u option and service name. For example:
journalctl -eu nginx.service
The output starts from the end. You can use the arrow keys on your keyboard to scroll logs. Also, you can view the service state change history using the journalctl command with the -xeu options. For example:
journalctl -xeu nginx.service
You can find more information about service management in the previous article.