Systemd for those who continue. Part 1 - Running Units on Timed Events

! systemd , ( , . ( ) . Manjaro Linux c systemd v247.2. ... . , , Β« -Β», , , !





Systemd β€” Linux, . , :





pacman -Ql $(pacman -Qsq systemd|xargs)|egrep '^systemd\s|^systemd-sysvcompat\s'|egrep "man/man[1|5|8]/[[:print:]]*\.gz"|wc -l
278
      
      



, systemd. , «»:





pacman -Ql $(pacman -Qsq systemd|xargs)|egrep '^systemd\s|^systemd-sysvcompat\s'|wc -l
1852
      
      



, . -, , !





Disclamer: ( Β«triggered byΒ»), , , . - , , , , .





, . .

, cron



, . 80-, , , . - . / , , . - , systemd ( *.timer



) (*.target



) ; - ; ; ( Unix , at



), - , , , .





… . , , man-db.timer :





$ cat /usr/lib/systemd/system/man-db.timer
[Unit]
Description=Daily man-db regeneration
Documentation=man:mandb(8)

[Timer]
OnCalendar=daily
AccuracySec=12h
Persistent=true

[Install]
WantedBy=timers.target
      
      



, . - , ? ! , [Timer]



Unit=



, , systemd *.service



. !





$ cat /usr/lib/systemd/system/man-db.service
[Unit]
Description=Daily man-db regeneration
Documentation=man:mandb(8)
ConditionACPower=true

[Service]
Type=oneshot
# Recover from deletion, per FHS.
ExecStart=+/usr/bin/install -d -o root -g root -m 0755 /var/cache/man
# Expunge old catman pages which have not been read in a week.
ExecStart=/usr/bin/find /var/cache/man -type f -name *.gz -atime +6 -delete
# Regenerate man database.
ExecStart=/usr/bin/mandb --quiet
User=root
Nice=19
IOSchedulingClass=idle
IOSchedulingPriority=7
      
      



, . 00:00 (OnCalendar=daily



) , 12 (AccuracySec=12h



), - , :





$ systemctl status man-db.timer 
● man-db.timer - Daily man-db regeneration
     Loaded: loaded (/usr/lib/systemd/system/man-db.timer; disabled; vendor preset: disabled)
     Active: active (waiting) since Thu 2020-12-31 23:18:59 MSK; 1 day 19h ago
    Trigger: Sun 2021-01-03 00:00:00 MSK; 5h 30min left
   Triggers: ● man-db.service
       Docs: man:mandb(8)

 31 23:18:59 dell-lnx systemd[1]: Started Daily man-db regeneration.
      
      



AccuracySec=



β€” 1us! , . , ( /etc/systemd/system.conf



: DefaultTimerAccuracySec=



) . , , [Timer]



, OnCalendar=



«» .





,

  • OnBootSec=



    .





  • OnStartupSec=



    , , .





  • OnActiveSec=



    . , , .





  • OnUnitActiveSec=



    .





  • OnUnitInactiveSec=



    . . . OnCalendar=



    .





  • RandomizedDelaySec=



    . , . -- .





  • OnClockChange=, OnTimezoneChange=



    , - . , , false



    .





  • Persistent=



    - . OnCalendar=



    . β€” false



    .





  • WakeSystem=



    . . . . , . , Β« Β». - true



    , ( ) .





  • RemainAfterElapse=



    , true



    , , false



    , . (Transient Units) . , , at



    .





, , ,

. , , cron , , . :





[Unit]
Description=Test timer

[Timer]
OnCalendar=01:00
OnActiveSec=1.5h
      
      



. - 13-… OnCalendar=Fri *-*-13 12:00:00



: Mon 2025-12-01 00:00:00.000000 Europe/Moscow



( ) - , : OnCalendar=yearly Asia/Kamchatka



( - ):

OnCalendar=*-01-01 00:00:00 Asia/Kamchatka



( ) :





                       minutely β†’ *-*-* *:*:00
                         hourly β†’ *-*-* *:00:00
                          daily β†’ *-*-* 00:00:00
                        monthly β†’ *-*-01 00:00:00
                         weekly β†’ Mon *-*-* 00:00:00
                         yearly β†’ *-01-01 00:00:00
                      quarterly β†’ *-01,04,07,10-01 00:00:00                                                      
                   semiannually β†’ *-01,07-01 00:00:00
      
      



:





@ β€” epoch time
        Fri 2012-11-23 11:12:13 β†’ Fri 2012-11-23 11:12:13
            2012-11-23 11:12:13 β†’ Fri 2012-11-23 11:12:13
        2012-11-23 11:12:13 UTC β†’ Fri 2012-11-23 19:12:13
                     2012-11-23 β†’ Fri 2012-11-23 00:00:00
                       12-11-23 β†’ Fri 2012-11-23 00:00:00
                       11:12:13 β†’ Fri 2012-11-23 11:12:13
                          11:12 β†’ Fri 2012-11-23 11:12:00
                            now β†’ Fri 2012-11-23 18:15:22
                          today β†’ Fri 2012-11-23 00:00:00
                      today UTC β†’ Fri 2012-11-23 16:00:00
                      yesterday β†’ Fri 2012-11-22 00:00:00
                       tomorrow β†’ Fri 2012-11-24 00:00:00
      tomorrow Pacific/Auckland β†’ Thu 2012-11-23 19:00:00
                       +3h30min β†’ Fri 2012-11-23 21:45:22
                            -5s β†’ Fri 2012-11-23 18:15:17
                      11min ago β†’ Fri 2012-11-23 18:04:22
                    @1395716396 β†’ Tue 2014-03-25 03:59:56
      
      



OnCalendar=



, .





:





      Sat,Thu,Mon..Wed,Sat..Sun β†’ Mon..Thu,Sat,Sun *-*-* 00:00:00
          Mon,Sun 12-*-* 2,1:23 β†’ Mon,Sun 2012-*-* 01,02:23:00
                        Wed *-1 β†’ Wed *-*-01 00:00:00
               Wed..Wed,Wed *-1 β†’ Wed *-*-01 00:00:00
                     Wed, 17:48 β†’ Wed *-*-* 17:48:00
    Wed..Sat,Tue 12-10-15 1:2:3 β†’ Tue..Sat 2012-10-15 01:02:03
                    *-*-7 0:0:0 β†’ *-*-07 00:00:00
                          10-15 β†’ *-10-15 00:00:00
            monday *-12-* 17:00 β†’ Mon *-12-* 17:00:00
      Mon,Fri *-*-3,1,2 *:30:45 β†’ Mon,Fri *-*-01,02,03 *:30:45
           12,14,13,12:20,10,30 β†’ *-*-* 12,13,14:10,20,30:00
                12..14:10,20,30 β†’ *-*-* 12..14:10,20,30:00
      mon,fri *-1/2-1,3 *:30:45 β†’ Mon,Fri *-01/2-01,03 *:30:45
                 03-05 08:05:40 β†’ *-03-05 08:05:40
                       08:05:40 β†’ *-*-* 08:05:40
                          05:40 β†’ *-*-* 05:40:00
         Sat,Sun 12-05 08:05:40 β†’ Sat,Sun *-12-05 08:05:40
               Sat,Sun 08:05:40 β†’ Sat,Sun *-*-* 08:05:40
               2003-03-05 05:40 β†’ 2003-03-05 05:40:00
     05:40:23.4200004/3.1700005 β†’ *-*-* 05:40:23.420000/3.170001
                 2003-02..04-05 β†’ 2003-02..04-05 00:00:00
           2003-03-05 05:40 UTC β†’ 2003-03-05 05:40:00 UTC
                     2003-03-05 β†’ 2003-03-05 00:00:00
                          03-05 β†’ *-03-05 00:00:00
                         hourly β†’ *-*-* *:00:00
                          daily β†’ *-*-* 00:00:00
                      daily UTC β†’ *-*-* 00:00:00 UTC
                        monthly β†’ *-*-01 00:00:00
                         weekly β†’ Mon *-*-* 00:00:00
        weekly Pacific/Auckland β†’ Mon *-*-* 00:00:00 Pacific/Auckland
                         yearly β†’ *-01-01 00:00:00
                       annually β†’ *-01-01 00:00:00
                          *:2/3 β†’ *-*-* *:02/3:00
      
      







. , :





  • *-*~01



    - The first day from the end of each month (it is the last day of the month).





  • *-05~05



    - May 27th each year (31-5).





  • Mon *-12~07/1



    - Last Monday in December.





  • Mon *-12-01/3



    - Third Monday in December.





You can check timestamps for validity using the utility systemd-analyze



:





$ systemd-analyze calendar 'Mon *-12-01/1'
  Original form: Mon *-12-01/1              
Normalized form: Mon *-12-01/1 00:00:00     
    Next elapse: Mon 2021-12-06 00:00:00 MSK
       (in UTC): Sun 2021-12-05 21:00:00 UTC
       From now: 11 months 2 days left

$ systemd-analyze timespan 1.5h
Original: 1.5h      
      ΞΌs: 5400000000
   Human: 1h 30min

$ systemd-analyze timestamp 01:00:30.9999
  Original form: 01:00:30.9999              
Normalized form: Sat 2021-01-02 01:00:30 MSK
       (in UTC): Fri 2021-01-01 22:00:30 UTC
   UNIX seconds: @1609538430.999900         
       From now: 18h ago 
      
      



So, in principle, everything is simple, logical and beautiful. And of course read:





man systemd.timer
man systemd.time
man systemd-system.conf
man systemd-analyze
man tzselect
      
      



List of Series Articles

  1. Why do business owners prefer bicycles over ready-made solutions? Or about systemd, part 0





  2. Systemd for those who continue. Part 1 - Running Units on Timed Events












All Articles