Professional Documents
Culture Documents
This tutorial shows you some basic commands that will be useful to manage services,
run levels, and logs. First, you might want to know the systemd version on your Linux
distribution, run
systemd --version
or
systemctl --version
Managing Services
Systemd can help you start, stop, restart services and also allows you to auto-start
services at boot time.
Stop a service
Sometimes, you may find an unknown process is running with the htop command. You
want to stop it, but don’t know which systemd service it belongs to. In this case, you
can run the following command
systemctl status
This command will give you a list of active systemd service units and the commands
used to start them.
runlevel
Use the following command to check the status of the default target, which by default is
usually the graphical target (run level 5).
You can view recent logs with the following command. You can press F key to jump to
the next page and press B key to going back to the previous page. Press Q to quit.
sudo journalctl
sudo journalctl -b
sudo journalctl -b -1
sudo journalctl -f
To view Postfix logs, run
sudo journalctl -u postfix
Some packages may still be configured to save logs to plain files such as the Postfix log
files on Ubuntu (/var/log/mail.log), so you can use a command-line text editor to
peruse the logs. Sometimes a systemd service might stop producing log messages, you
can try to restart rsyslog to solve this problem.
When two services shouldn’t run at the same time, you can use
the Conflicts parameter. For example, I always use a VPN on my Ubuntu desktop
computer. It’s a self-hosted VPN running on a Vultr VPS. Then I sometimes use
Transmission to download torrents. When I start Transmission, I must close the VPN
connection, because Vultr will shut down your account if you use their servers to
download torrents (I mean downloading copyright-infringing materials via BitTorrent).
Conflicts=openconnect.service
Save and close the file. Then reload systemd for the changes to take effect.
If you want service B to start when service A starts, you can use the Wants parameter
to make it happen. In the [Unit] section of a systemd .service file, add a line like
this:
[Unit]
Wants = b.service
[Unit]
Wants = b.service
Before = b.service
Sometimes, you may want service B to wait for service A to complete before starting
itself. Then you can add the following lines in the [Service] section of
the b.service file.
[Service]
ExecStartPre=/bin/sleep 20
If you are running a Linux distro that uses Systemd, you may find that your commands
in /etc/rc.local file would not run at system boot time. The following tutorial
explains how to enable /etc/rc.local script to run on system startup.
Accounting
Systemd allows you to check how much CPU and memory is used by a service and how
many tasks are running by the service. If you are using a recent Linux distro
like Ubuntu 20.04, Debian 10, RHEL 8, accounting is enabled by default. On old
Linux distros like Ubuntu 18.04, you need to manually enable accounting. Let’s say you
want to enable accounting for Nginx on Ubuntu 18.04, then follow the instruction
below.
[Service]
CPUAccounting=true
MemoryAccounting=true
TasksAccounting=true
There’s no need to restart Nginx. Now you can check Nginx status with:
You may have seen a template systemd service that contains the @ symbol like
the Syncthing service.
/lib/systemd/system/syncthing@.service
This kind of service allows you to run multiple instances. For example, you can run
Syncthing for two users with:
If you want to add custom configuration to this kind of service, you need to create a
folder for each of these services.
sudo nano
/etc/systemd/system/syncthing@user1.service.d/custom.conf
sudo nano
/etc/systemd/system/syncthing@user2.service.d/custom.conf
That’s it! Subscribe to our free newsletter to get the latest Linux tutorials. You can also
follow us on Twitter or like our Facebook page.
https://www.linuxbabe.com/command-line/systemd-services-run-levels-logs