Over the last few years, Linux and its distributions have been transformed through the use of systemd as the init system. This article will introduce systemd commands and create a service and run it with systemctl.
Basic Unit Management
Systemd manages the units . Units come in 12 different types, but the most commonly used type is a service.
To manage service in Linux, we use the systemctl tool
Start service
1 2 | $ sudo systemctl start nginx.service |
Stop service
1 2 | $ sudo systemctl stop nginx.service |
Restart service
1 2 | $ sudo systemctl restart nginx.service |
Reload service
1 2 | $ sudo systemctl reload nginx.service |
By default, services in linux are not automatically turned on when starting the operating system. To do that, you need to enable the service
1 2 | $ sudo systemctl enable nginx.service |
Disable service
1 2 | $ sudo systemctl disable nginx.service |
Getting an Overview of the System State
We can view unit information with systemctl
- List units files are systemd list “active”
1 2 | $ systemctl list-units |
io
- List of unit files loaded into memory but currently not active
1 2 | $ systemctl list-units --all |
- List of unit files that have not been loaded into memory
1 2 | $ systemctl list-unit-files |
Unit States and Logs
To see the status of the service units you can use the status option of systemctl
1 2 | systemctl status nginx.service |
To view all unit log entries
1 2 | $ journalctl -u nginx.service |
If you only want to list the entries of the current boot session, add the -b option
1 2 | $ journalctl -b -u nginx.service |
Inspecting Units and Unit Files
Now we know how to adjust the state of the unit by starting or stopping the service through systemctl.
The unit file contains the parameters that systemd uses to manage and run the unit. To see its full information:
1 2 | $ systemctl cat nginx.service |
To see which units run on nginx boot
1 2 | $ systemctl list-dependencies nginx.service |
show recursive
1 2 | $ systemctl list-dependencies --all nginx.service |
Finally, to see specific information about the low level of unit settings (the values of the parameters are controlled by systemd).
1 2 | $ systemctl show nginx.service |
Modify unit file
We can edit the unit file without having to go into the directory to find it
Append adds an option to the unit file
1 2 | $ sudo systemctl edit nginx.service |
Directly edit all file contents
1 2 | $ sudo systemctl edit --full nginx.service |
Reload systemd again after fixing
1 2 | $ sudo systemctl daemon-reload |
Use Systemd to start service at boot
We will try to create a custom service, run it on boot with systemd
The service file will run the following shell script
1 2 3 4 5 6 7 8 9 10 11 | test_service.sh <span class="token assign-left variable">DATE</span> <span class="token operator">=</span> <span class="token variable"><span class="token variable">`</span> <span class="token function">date</span> <span class="token string">'+%Y-%m-%d %H:%M:%S'</span> <span class="token variable">`</span></span> <span class="token builtin class-name">echo</span> <span class="token string">"Example service started at <span class="token variable">${DATE}</span> "</span> <span class="token operator">|</span> systemd-cat -p info <span class="token keyword">while</span> <span class="token builtin class-name">:</span> <span class="token keyword">do</span> <span class="token builtin class-name">echo</span> <span class="token string">"Looping..."</span> <span class="token punctuation">;</span> <span class="token function">sleep</span> <span class="token number">30</span> <span class="token punctuation">;</span> <span class="token keyword">done</span> |
Copy the script file to the bin directory
1 2 3 | $ sudo cp test_service.sh /usr/bin/test_service.sh $ sudo chmod +x /usr/bin/test_service.sh |
Create a unit file (for full options, please refer to https://www.freedesktop.org/software/systemd/man/systemd.service.html )
1 2 3 4 5 6 7 8 9 10 11 12 | File: /lib/systemd/system/myservice.service [Unit] Description=Example systemd service. [Service] Type=simple ExecStart=/bin/bash /usr/bin/test_service.sh [Install] WantedBy=multi-user.target |
- Start & see status
1 2 | $ sudo systemctl start myservice |
1 2 | sudo systemctl status myservice |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ● myservice.service - Example systemd service. Loaded: loaded (/lib/systemd/system/myservice.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2018-05-01 18:17:14 UTC; 4s ago Main PID: 16266 (bash) Tasks: 2 Memory: 748.0K CPU: 4ms CGroup: /system.slice/myservice.service ├─16266 /bin/bash /usr/bin/test_service.sh └─16270 sleep 30 May 01 18:17:14 localhost systemd[1]: Started Example systemd service.. May 01 18:17:14 localhost cat[16269]: Example service started at 2018-05-01 18:17:14 May 01 18:17:14 localhost bash[16266]: Looping... |
- Enable service on boot
1 2 | $ sudo systemctl enable myservice |