Bboysoul's Blog

首页 公告 RSS

Ingenious Applications of systemd

February 4, 2024 本文有 519 个字 需要花费 3 分钟阅读

Introduction

systemd, beyond its capacity to manage various services within the system, harbors a plethora of intriguing and practical features. In this blog post, I will elucidate how to employ systemd for time synchronization, disk mounting, and execution of scheduled tasks.

Time Synchronization: Utilizing systemd-timesyncd

systemd-timesyncd is a straightforward network time synchronization daemon that can be employed to synchronize your system’s time. Initially, we need to disable the local NTP service, such as chrony that I use on my machine:

systemctl disable chrony && systemctl stop  chrony

Subsequently, we can use the timedatectl status command to inspect the status of the local NTP service. Following this, we need to check the status of the systemd-timesyncd service:

systemctl status systemd-timesyncd

If you encounter the following output:

○ systemd-timesyncd.service
     Loaded: masked (Reason: Unit systemd-timesyncd.service is masked.)
     Active: inactive (dead)

It implies that your service is masked. To unmask it, input the following command:

systemctl unmask systemd-timesyncd

Then initiate the service:

systemctl start systemd-timesyncd

If the prompt Failed to start systemd-timesyncd.service: Unit systemd-timesyncd.service not found. appears, it indicates that the systemd-timesyncd package is not installed in your system, which can be installed using the following command:

apt-get install systemd-timesyncd

After the installation is complete, initiate the service:

systemctl start systemd-timesyncd

If you wish to modify the NTP server, you can edit the /etc/systemd/timesyncd.conf file:

vim /etc/systemd/timesyncd.conf

Modify it to:

[Time]
NTP=cn.ntp.org.cn

Finally, restart the service and configure it to start upon boot:

systemctl restart systemd-timesyncd
systemctl enable systemd-timesyncd

Lastly, check the status:

timedatectl

Disk Mounting: Employing systemd to mount NFS

To utilize systemd for disk mounting, you need to create a .mount file. For instance, we create a file named root-test.mount:

vim /etc/systemd/system/root-test.mount

And add the following content:

[Unit]
Description=Mount NFS

[Mount]
What=10.10.100.244:/volume1/data
Where=/root/test
Type=nfs
Options=defaults

[Install]
WantedBy=multi-user.target

Note that the filename of the .mount file must align with the mounting path. For instance, if the mounting path address is /root/test, then the filename must be root-test.mount. Otherwise, you would encounter the following error message:

nfs-test.mount: Where= setting doesn't match unit name. Refusing.

After creation, use the following command to start the mount:

systemctl start root-test.mount

You can use the systemctl stop root-test.mount command to unmount the file system and the systemctl enable root-test.mount command to set the boot start mount.

Execution of Scheduled Tasks: Using systemd Timer

systemd can also be configured to execute scheduled tasks. Firstly, we need to create a service. For instance, we create a file named date.service:

vim /etc/systemd/system/date.service

And add the following content:

[Unit]
Description=log date
Wants=logdate.timer

[Service]
Type=oneshot
ExecStart=sh -c 'date > /var/date.log'

[Install]
WantedBy=multi-user.target

You can use the systemctl status logdate command to check the status of the service.

Next, we need to create a timer. For instance, we create a file named logdate.timer:

vim /etc/systemd/system/logdate.timer

And add the following content:

[Unit]
Description=Exec logmemory
Requires=date.service

[Timer]
Unit=date.service
OnCalendar=*-*-* *:*:00

[Install]
WantedBy=timers.target

I have configured it here to execute the logmemory.service service every minute.

Start this timer:

systemctl start logdate.timer

You can use the systemctl status logdate.timer command to check the status of the timer.

Feel free to follow my blog at www.bboy.app

Have Fun


Tags:

本站总访问量 本站总访客数