Systemd¶
- Operating system and its daemons can be managed with systemd program
- Like mentioned earlier, systemd init is the process which is started first during system bootup and from which every other process is inherited
- Systemd is started with a PID value of 1
-
Systemd will do the following operations during system bootup:
- Start the most essential system programs
- Start task scheduler
- Collect log information
- etc.
-
Most Linux distributions now include systemd
- systemctl is a command which is used for daemon management
-
Below is a set of examples of systemctl command usage
-
Example 1: Check the status of daemon
testuser@ubuntu-pc:~$ systemctl status systemd-networkd
- Example 2: Stop and start the daemon
testuser@ubuntu-pc:~$ systemctl stop systemd-networkd
testuser@ubuntu-pc:~$ systemctl start systemd-networkd
- Example 3: Setting daemon to start during system bootup and removing daemon from system bootup sequence
testuser@ubuntu-pc:~$ systemctl enable systemd-networkd
testuser@ubuntu-pc:~$ systemctl disable systemd-networkd
- Example 4: Check if daemon is set to start during system bootup
testuser@ubuntu-pc:~$ systemctl is-enabled systemd-networkd
- Example 5: Check if daemon is active at the moment
testuser@ubuntu-pc:~$ systemctl is-active systemd-networkd
- All system units can be listed with systemctl list-unit-files command
testuser@ubuntu-pc:~$ systemctl list-unit-files
UNIT FILE STATE VENDOR PRESET
proc-sys-fs-binfmt_misc.automount static enabled
-.mount generated enabled
boot-efi.mount generated enabled
dev-hugepages.mount static enabled
dev-mqueue.mount static enabled
proc-sys-fs-binfmt_misc.mount disabled enabled
run-vmblock\x2dfuse.mount enabled enabled
snap-core18-1880.mount enabled enabled
snap-gnome\x2d3\x2d34\x2d1804-36.mount enabled enabled
...
- If user does any changes to unit files, these changes can be applied without restarting the service with the following command
testuser@ubuntu-pc:~$ systemctl daemon-reload
- The benefit here is that port which daemon listens remains open to client side without interruption
Systemd timers¶
- Timers are optional way to schedule tasks in operating system
-
Systemd timer has manu benefits compared to older crontab scheduling
- Automatic generation of log information
- Configuration of maximum processor and memory usage (for example 50 % load at maximum)
- Task segmentation for smaller pieces and utilization of other systemd units makes creating complicated tasks possible
-
Below are two examples, one with older crontab and another with systemd
- Example: Create a scheduled task, which saves date information into /tmp/date file every 10 minutes
- Crontab
*/10 **** /usr/bin/date >> /tmp/date
Systemd - Let's create a file /etc/systemd/system/date.service with the following content:
[Unit]
Description=Date information will be printed into the /tmp/date file
[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c '/usr/bin/date >> /tmp/date'
- Then let's create a timer file /etc/systemd/system/date.timer with the following content:
[Unit]
Description=date.service is run every 10 minutes
[Timer]
OnCalendar=*:0/10
- Lastly, start timer with the following command:
testuser@ubuntu-PC:~$ systemctl start date.timer
- Task can now be monitored by using command systemctl list-timers
testuser@ubuntu-PC:~$ systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Thu 2019-08-29 17:00:00 UTC 8min left Thu 2019-08-29 16:50:06 UTC 1min 7s ago date.timer date.service
Thu 2019-08-29 17:19:32 UTC 28min left Thu 2019-08-29 10:47:57 UTC 6h ago motd-news.timer motd-news.service
Fri 2019-08-30 04:11:22 UTC 11h left Thu 2019-08-29 07:14:28 UTC 9h ago apt-daily.timer apt-daily.service
Fri 2019-08-30 06:08:15 UTC 13h left Thu 2019-08-29 06:49:04 UTC 10h ago apt-daily-upgrade.timer apt-daily-upgrade.service
Fri 2019-08-30 16:16:54 UTC 23h left Thu 2019-08-29 16:16:54 UTC 34min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2019-09-02 00:00:00 UTC 3 days left Mon 2019-08-26 15:40:35 UTC 3 days ago fstrim.timer fstrim.service
6 timers listed.
Pass --all to see loaded but inactive timers, too.