I previous article in the the locate file search commands, and rely updatedb update the index to quickly locate files, thus requiring timed run this command to update the file index. crontab is usually used to create timing tasks on Linux and Unix-like systems.

apt install mlocate on Ubuntu, we will install a script file to /etc/cron.daily/mlocate , which is to execute updatedb every day through the Cron mechanism. However, on my openSUSE , there is no related Crontab configuration, but I found that the index file is updated at zero every day. Then who is going to execute this timing task?

I searched for files related to mlocate and found the following files:

❯ locate "mlocate"
/etc/systemd/system/timers.target.wants/mlocate.timer
/usr/lib/systemd/system/mlocate.service
/usr/lib/systemd/system/mlocate.timer

Originally, on the openSUSE system, the timing unit Systemd Systemd is a Linux system service management program, which was introduced in my previous article using SYSTEMCTL management system service on OPENSUSE.

So here we will focus on the systemd timer service (systemd timer unit).

systemd timing unit

Similar to Cron, the timing unit of systemd provides a mechanism to schedule tasks on Linux systems. Compared with the Cron mechanism, it has the following features (on systems that use systemd as initialization and service management):

  • Scheduled tasks can depend on other systemd services
  • You can use the systemctl command to manage the timing unit, similar to the management of systemd services
  • In addition to Cron-like cyclic real-time timing tasks (realtime), it also supports a task that is triggered based on non-time events (monotonic)
  • The timing unit records logs to systemd's journal system (journal), so it is convenient for unified monitoring and diagnosis

Types of systemd scheduled tasks

In the above features, we mentioned that it supports two types-realtime and monotonic

  • Realtime-similar to Cron, this type of timing task is triggered by a defined absolute time, defined in the configuration file through the OnCalendar option
  • Monotonic-This type of timing task will be triggered after a certain period of time after a specified event (such as system startup, service activation), defined in the configuration file by OnBootSec and OnUnitActiveSec , OnStartupSec , and the trigger time of this type of timing task Not fixed, it will be reset every time the system restarts

Systemd timing task configuration

At the beginning of the article, we are looking for regular tasks mlocate update file index to see that there are documents /usr/lib/systemd/system/mlocate.timer , yes, that is by .timer as systemd extension Unit file to define the timing unit of systemd

[Unit]
Description=Daily locate database update
Documentation=man:updatedb

[Timer]
OnCalendar=daily
AccuracySec=12h
Unit=mlocate.service
Persistent=true

[Install]
WantedBy=timers.target

You can see that the file format is similar to the unit file of the systemd service, but it requires the [Timer] section, in which the following options are defined

  • OnCalendar=daily, meaning to trigger every day
  • AccuracySec=12h, which means that the execution time needs to be speculated for some reason
  • Unit=mlocate.service, here is the task service that needs to be executed
  • Persistent=true, specifies that if the task can be executed due to shutdown and other reasons, the startup will trigger the task immediately

Then the timing unit specifies mlocate.service as the task to trigger execution, that is /usr/lib/systemd/system/mlocate.service , and the service is defined to use the updatedb command to update the file index .

For OnCalendar , the format supported by its value is DayOfWeek Year-Month-Day Hour:Minute:Second , for example

  • OnCalendar= - -* 5:00:00, designated to be executed at 5 o'clock every morning
  • OnCalendar=Fri 18:00:00, designated to be executed at 6 pm on Friday of each month

In a configuration file, you can also specify multiple OnCalendar , for example

OnCalendar=Mon..Fri 10:00
OnCalendar=Sat,Sun 22:00

The above configuration specifies that it will be executed at 10 am every day from Monday to Friday, and at 10 pm on the two weekends.

Let's give an example of using monotonic type timing tasks, in the directory /etc/systemd/system/ the service unit file foo.service

[Unit]
Description="Foo shell script"

[Service]
ExecStart=/usr/local/bin/foo.sh

At the same time create a timing unit file foo.timer

[Unit]
Description="Run foo shell script"

[Timer]
OnBootSec=5min
OnUnitActiveSec=24h
Unit=foo.service

[Install]
WantedBy=multi-user.target

Here we see that in [Timer] section, we define the options

  • OnBootSec=5min, which specifies that the execution of the specified service is triggered 5 minutes after the system starts
  • OnUnitActiveSec=24h, which specifies that the service will be executed 24 hours after activation, that is, it will be executed once a day (but the specific time of execution depends on the boot time)
  • Unit=foo.service, specifies that the triggered task is the foo service we defined above, which is to execute the script

Management timer unit

In the above features, we said that the timer unit can systemctl command, similar to the management service unit

  • sudo systemctl start foo.timer , start the specified timing unit
  • sudo systemctl enable foo.timer , turn on the timing unit to automatically activate (start automatically after booting)
  • sudo systemctl list-timers , lists the activated timing units of the current system

E.g

❯ sudo systemctl list-timers
NEXT                         LEFT                LAST                         PASSED      UNIT                         ACTIVATES
Fri 2021-12-03 17:00:00 CST  22min left          Fri 2021-12-03 16:00:03 CST  37min ago   snapper-timeline.timer       snapper-timeline.service
Sat 2021-12-04 00:00:00 CST  7h left             Fri 2021-12-03 00:00:03 CST  16h ago     logrotate.timer              logrotate.service
Sat 2021-12-04 00:00:00 CST  7h left             Fri 2021-12-03 00:00:03 CST  16h ago     mandb.timer                  mandb.service
Sat 2021-12-04 00:00:00 CST  7h left             Fri 2021-12-03 00:00:03 CST  16h ago     mlocate.timer                mlocate.service

We can also directly use journalctl to view related logs, for example

❯ sudo journalctl -u mlocate
-- Logs begin at Thu 2021-12-02 06:58:59 CST, end at Fri 2021-12-03 16:41:26 CST. --
Dec 03 00:00:03 linux-dev systemd[1]: Starting Update locate database...
Dec 03 00:00:03 linux-dev su[864]: (to nobody) root on none
Dec 03 00:00:06 linux-dev systemd[1]: Started Update locate database.

At the same time, check the log of the mlocate timing unit and service.

For more configuration details, please refer to official document .

Summarize

If your system uses Systemd as the initialization and service management system, and you want to use the features we mentioned earlier, then we can use the systemd timer unit to define our timing tasks. Of course, most systems still support the timing tasks of the Crontab mechanism.

Also posted on Mengz's Blog

梦哲
74 声望58 粉丝

寻找人生的意义!