Difference between revisions of "Systemd/Timers"

From ArchWiki
Jump to: navigation, search
m (The mandb service: service name, description)
m (Fix mispelled 'OnCalender')
(17 intermediate revisions by 7 users not shown)
Line 2: Line 2:
 
[[Category:Daemons and system services]]
 
[[Category:Daemons and system services]]
 
[[Category:Boot process]]
 
[[Category:Boot process]]
{{Article summary start}}
+
{{Related articles start}}
{{Article summary text|Covers how to replace cron by systemd.}}
+
{{Related|systemd}}
{{Article summary heading|Related}}
+
{{Related|systemd/User}}
{{Article summary wiki|systemd}}
+
{{Related|systemd/Services}}
{{Article summary wiki|systemd/User}}
+
{{Related|systemd FAQ}}
{{Article summary wiki|systemd/Services}}
+
{{Related|cron}}
{{Article summary wiki|systemd FAQ}}
+
{{Related articles end}}
{{Article summary wiki|cron}}
+
{{Article summary end}}
+
  
 
[[Systemd]] is capable of taking on a significant subset of the functionality of [[Cron]] through built in support for calendar time events (from systemd version 197) as well as monotonic time events.
 
[[Systemd]] is capable of taking on a significant subset of the functionality of [[Cron]] through built in support for calendar time events (from systemd version 197) as well as monotonic time events.
Line 19: Line 17:
  
 
* Last status and logging outputs can be got through journalctl. This enables proper debugging
 
* Last status and logging outputs can be got through journalctl. This enables proper debugging
* Systemd has a lot of options which are useful for setting the environment for the job ot be done properly (eg IOSchedulingPriority, Nice or JobTimeoutSec)
+
* Systemd has a lot of options which are useful for setting the environment for the job to be done properly (eg IOSchedulingPriority, Nice or JobTimeoutSec)
 
* These jobs can be made to depend on other ''systemd'' units if required
 
* These jobs can be made to depend on other ''systemd'' units if required
  
While you do lose support for ''anacron''; ''systemd'' timer, target and service files can fill up for the rest of ''cron'' very easily.
+
== Hourly, daily, weekly, monthly and yearly events ==
 
+
== Hourly, daily and weekly events ==
+
  
 
One strategy which can be used for recreating this functionality is through timers which call in targets. All services which need to be run hourly can be called in as dependencies of these targets. The strategy mentioned here has been detailed first in [http://blog.higgsboson.tk/2013/06/09/use-systemd-as-a-cron-replacement/ this blogpost].
 
One strategy which can be used for recreating this functionality is through timers which call in targets. All services which need to be run hourly can be called in as dependencies of these targets. The strategy mentioned here has been detailed first in [http://blog.higgsboson.tk/2013/06/09/use-systemd-as-a-cron-replacement/ this blogpost].
Line 32: Line 28:
 
First, the creation of a few directories is required:
 
First, the creation of a few directories is required:
  
  # mkdir /etc/systemd/system/timer-{hourly,daily,weekly}.target.wants
+
  # mkdir /etc/systemd/system/timer-{hourly,daily,weekly,monthly,yearly}.target.wants
  
 
The following files will need to be created in the paths specified in order for this to work.
 
The following files will need to be created in the paths specified in order for this to work.
Line 77: Line 73:
 
StopWhenUnneeded=yes
 
StopWhenUnneeded=yes
 
</nowiki>}}
 
</nowiki>}}
 +
 +
Systemd does not currently reschedule timers based on the last time they were run if the system is rebooted in between, i.e. it does not implement an anacron replacement. However, anacron can be used in conjunction with Systemd by updating the anacron timestamps through an hourly Systemd timer.
  
 
=== Weekly events ===
 
=== Weekly events ===
Line 86: Line 84:
 
[Timer]
 
[Timer]
 
OnBootSec=15min
 
OnBootSec=15min
OnUnitActiveSec=1w
+
OnCalendar=weekly
 
Unit=timer-weekly.target
 
Unit=timer-weekly.target
  
Line 96: Line 94:
 
[Unit]
 
[Unit]
 
Description=Weekly Timer Target
 
Description=Weekly Timer Target
 +
StopWhenUnneeded=yes
 +
</nowiki>}}
 +
 +
=== Monthly events ===
 +
 +
{{hc|/etc/systemd/system/timer-monthly.timer|<nowiki>
 +
[Unit]
 +
Description=Monthly Timer
 +
 +
[Timer]
 +
OnBootSec=45min
 +
OnCalendar=monthly
 +
Unit=timer-monthly.target
 +
 +
[Install]
 +
WantedBy=basic.target
 +
</nowiki>}}
 +
 +
{{hc|/etc/systemd/system/timer-monthly.target|<nowiki>
 +
[Unit]
 +
Description=Monthly Timer Target
 +
StopWhenUnneeded=yes
 +
</nowiki>}}
 +
 +
=== Yearly events ===
 +
 +
{{hc|/etc/systemd/system/timer-yearly.timer|<nowiki>
 +
[Unit]
 +
Description=Yearly Timer
 +
 +
[Timer]
 +
OnBootSec=90min
 +
OnCalendar=yearly
 +
Unit=timer-yearly.target
 +
 +
[Install]
 +
WantedBy=basic.target
 +
</nowiki>}}
 +
 +
{{hc|/etc/systemd/system/timer-yearly.target|<nowiki>
 +
[Unit]
 +
Description=Yearly Timer Target
 
StopWhenUnneeded=yes
 
StopWhenUnneeded=yes
 
</nowiki>}}
 
</nowiki>}}
Line 122: Line 162:
 
If you wish to completely migrate away from ''cron'' to ''systemd'', then it is prudent to have these services up and running for your system to remain workable.
 
If you wish to completely migrate away from ''cron'' to ''systemd'', then it is prudent to have these services up and running for your system to remain workable.
  
==== Logrotate ====
+
==== logrotate ====
 +
 
 +
This service runs {{ic|logrotate}}, which rotates, compresses and mails system logs. It relies upon package {{Pkg|logrotate}} which by default provides a cron job for this task ({{ic|/etc/cron.daily/updatedb}}). See {{ic|man logrotate}} for details.
  
 
{{hc|/etc/systemd/system/timer-daily.target.wants/logrotate.service|<nowiki>
 
{{hc|/etc/systemd/system/timer-daily.target.wants/logrotate.service|<nowiki>
Line 135: Line 177:
 
</nowiki>}}
 
</nowiki>}}
  
==== Update man-db ====
+
==== man-db ====
 +
 
 +
This service runs {{ic|mandb}}, which creates or updates the manual page index caches. It relies upon package {{Pkg|man-db}} which by default provides a cron job for this task ({{ic|/etc/cron.daily/man-db}}). See {{ic|man mandb}} for details.
  
 
{{hc|/etc/systemd/system/timer-daily.target.wants/man-db-update.service|<nowiki>
 
{{hc|/etc/systemd/system/timer-daily.target.wants/man-db-update.service|<nowiki>
Line 148: Line 192:
 
</nowiki>}}
 
</nowiki>}}
  
==== Update mlocate database ====
+
==== shadow ====
  
{{hc|/etc/systemd/system/timer-daily.target.wants/mlocate-update.service|<nowiki>
+
This service runs {{ic|pwck}} and {{ic|grpck}}, which together verify integrity of password and group files ({{ic|/etc/{passwd,shadow,group,gshadow} }}). Those tools are provided by the {{Pkg|shadow}} package, which by default provides a cron job for this task ({{ic|/etc/cron.daily/shadow}}). See {{ic|man shadow}}, {{ic|man pwck}} and {{ic|man grpck}} for details.
[Unit]
+
Description=Update mlocate database
+
 
+
[Service]
+
Nice=19
+
IOSchedulingClass=2
+
IOSchedulingPriority=7
+
ExecStart=/usr/bin/updatedb
+
</nowiki>}}
+
 
+
==== Verify integrity of password and group files ====
+
  
 
{{hc|/etc/systemd/system/timer-daily.target.wants/verify-shadow.service|<nowiki>
 
{{hc|/etc/systemd/system/timer-daily.target.wants/verify-shadow.service|<nowiki>
Line 175: Line 208:
 
=== Enable and start the timers ===
 
=== Enable and start the timers ===
  
  # systemctl enable timer-{hourly,daily,weekly}.timer && systemctl start timer-{hourly,daily,weekly}.timer
+
  # systemctl enable timer-{hourly,daily,weekly,monthly,yearly}.timer  
 +
# systemctl start timer-{hourly,daily,weekly,monthly,yearly}.timer
  
 
== Starting events according to the calendar ==
 
== Starting events according to the calendar ==
Line 199: Line 233:
 
{{Note|These example scripts assume that you have created the hourly, daily and weekly timers as described above.}}
 
{{Note|These example scripts assume that you have created the hourly, daily and weekly timers as described above.}}
  
=== The pkgstats service ===
+
=== Reflector ===
  
If you have the {{Pkg|pkgstats}} package installed, this service will be necessary in order to send data back to the Arch servers.
+
This service file may be used to update pacman's [[Mirrors|mirrorlist]] daily using the [[reflector]] script.
  
{{Tip|It should be noted that the user {{ic|nobody}}, which is being used here, is already present on all Arch systems. The use of this user is recommended for all services which can function in a completely unprivileged environment.}}
+
{{hc|/etc/systemd/system/timer-daily.target.wants/reflector.service|<nowiki>
 
+
{{hc|/etc/systemd/system/timer-weekly.target.wants/pkgstats.service|<nowiki>
+
 
[Unit]
 
[Unit]
Description=Run pkgstats
+
Description=Update the mirrorlist
  
 
[Service]
 
[Service]
User=nobody
+
Nice=19
ExecStart=/usr/bin/pkgstats
+
IOSchedulingClass=2
 +
IOSchedulingPriority=7
 +
ExecStart=/usr/bin/reflector --protocol http --latest 30 --number 20 --sort rate --save /etc/pacman.d/mirrorlist
 
</nowiki>}}
 
</nowiki>}}
  
=== The modprobed_db service ===
+
{{note|1=Reflector's options should be tweaked according to users' criteria. Use case examples may be found in [https://bbs.archlinux.org/viewtopic.php?pid=808753#p808753 this forum post] by Moderator/TU Xyne.}}
  
This service is of great use to people who compile their own kernels because it reduces compilation time by a significant amount. Refer to the [[Modprobed_db]] page for further details.
+
=== pkgstats ===
  
{{hc|/etc/systemd/system/timer-daily.target.wants/modprobed_db.service|<nowiki>
+
If you have [[pkgstats]] installed, this service will be necessary in order to send data back to the Arch servers.
 +
 
 +
{{Tip|It should be noted that the user {{ic|nobody}}, which is being used here, is already present on all Arch systems. The use of this user is recommended for all services which can function in a completely unprivileged environment.}}
 +
 
 +
{{hc|/etc/systemd/system/timer-weekly.target.wants/pkgstats.service|<nowiki>
 
[Unit]
 
[Unit]
Description=Run modprobed_db
+
Description=Run pkgstats
  
 
[Service]
 
[Service]
User=</nowiki>''enter user here''<nowiki>
+
User=nobody
ExecStart=/usr/bin/modprobed_db store
+
ExecStart=/usr/bin/pkgstats -q
 
</nowiki>}}
 
</nowiki>}}
  
=== The reflector service ===
+
=== Update mlocate database ===
  
This service file may be used to upgrade the mirrorlist daily using the [[Reflector]] program.
+
This service runs ''updatedb'', which updates the [[locate|mlocate]] database. The package {{Pkg|mlocate}} provides by default a cron job for this task ({{ic|/etc/cron.daily/updatedb}}). See {{ic|man 8 updatedb}} for details.
  
{{hc|/etc/systemd/system/timer-daily.target.wants/reflector.service|<nowiki>
+
{{hc|/etc/systemd/system/timer-daily.target.wants/mlocate-update.service|<nowiki>
 
[Unit]
 
[Unit]
Description=Update the mirrorlist
+
Description=Update mlocate database
  
 
[Service]
 
[Service]
Line 239: Line 277:
 
IOSchedulingClass=2
 
IOSchedulingClass=2
 
IOSchedulingPriority=7
 
IOSchedulingPriority=7
ExecStart=/usr/bin/reflector --protocol http --latest 30 --number 20 --sort rate --save /etc/pacman.d/mirrorlist
+
ExecStart=/usr/bin/updatedb -f proc
 
</nowiki>}}
 
</nowiki>}}
  
=== The man-db service ===
+
=== The modprobed_db service ===
  
This service runs ''mandb'', which updates the manual page index caches. See {{ic|man 8 mandb}} for details. The package {{Pkg|man-db}} provides a cron job for this task (file {{ic|/etc/cron.daily/man-db}}).
+
This service is of great use to people who compile their own kernels because it reduces compilation time by a significant amount. Refer to the [[Modprobed_db]] page for further details.
  
{{hc|/etc/systemd/system/timer-daily.target.wants/man-db.service|<nowiki>
+
{{hc|/etc/systemd/system/timer-daily.target.wants/modprobed_db.service|<nowiki>
 
[Unit]
 
[Unit]
Description=Update manual page index caches
+
Description=Run modprobed_db
  
 
[Service]
 
[Service]
Nice=19
+
User=</nowiki>''enter user here''<nowiki>
IOSchedulingClass=2
+
ExecStart=/usr/bin/modprobed_db store
IOSchedulingPriority=7
+
ExecStart=/usr/bin/mandb --quiet
+
 
</nowiki>}}
 
</nowiki>}}
  
=== The shadow service ===
+
=== The hosts-update service ===
  
This service verifies integrity of password and group files ({{ic|/etc/{passwd,shadow,group,gshadow} }}). The package {{Pkg|shadow}} provides a cron job for this task (file {{ic|/etc/cron.daily/shadow}}).
+
{{Expansion|missing package requirements, is it {{AUR|hosts-update}}?}}
  
{{hc|/etc/systemd/system/timer-daily.target.wants/shadow.service|<nowiki>
+
Updates {{ic|/etc/hosts}} with the mvps blocklist.
 +
 
 +
{{hc|/etc/systemd/system/timer-daily.target.wants/hosts-update.service|<nowiki>
 
[Unit]
 
[Unit]
Description=Verify integrity of password and group files
+
Description=Update hosts file
 +
After=network.target
  
 
[Service]
 
[Service]
Type=oneshot
+
Nice=19
ExecStart=/usr/bin/pwck -r
+
IOSchedulingClass=2
ExecStart=/usr/bin/grpck -r
+
IOSchedulingPriority=7
 +
ExecStart=/usr/bin/hosts-update
 +
 
 +
[Install]
 +
WantedBy=timer-daily.target
 
</nowiki>}}
 
</nowiki>}}
  
 
== See also ==
 
== See also ==
  
* https://fedoraproject.org/wiki/Features/SystemdCalendarTimers - systemd calendar timers on the Fedora Project wiki]
+
* https://fedoraproject.org/wiki/Features/SystemdCalendarTimers - systemd calendar timers on the Fedora Project wiki

Revision as of 20:06, 19 February 2014

Systemd is capable of taking on a significant subset of the functionality of Cron through built in support for calendar time events (from systemd version 197) as well as monotonic time events.

Introduction

While Cron has been a stalwart on the Linux landscape for years, it still provides no way to detect job failures, establish job dependencies, or allocate processes to cgroups. If you require any of this functionality, systemd provides a good structure to set up job scheduling. While doing so is slightly more cumbersome than relying on dcron or cronie, the benefits are not insignificant:

  • Last status and logging outputs can be got through journalctl. This enables proper debugging
  • Systemd has a lot of options which are useful for setting the environment for the job to be done properly (eg IOSchedulingPriority, Nice or JobTimeoutSec)
  • These jobs can be made to depend on other systemd units if required

Hourly, daily, weekly, monthly and yearly events

One strategy which can be used for recreating this functionality is through timers which call in targets. All services which need to be run hourly can be called in as dependencies of these targets. The strategy mentioned here has been detailed first in this blogpost.

Note: There is an AUR package systemd-cronAUR which uses a slightly different strategy to do what is being done here. If you wish to use this package, then the following scripts might need to be modified and their locations changed for them to work properly.

First, the creation of a few directories is required:

# mkdir /etc/systemd/system/timer-{hourly,daily,weekly,monthly,yearly}.target.wants

The following files will need to be created in the paths specified in order for this to work.

Hourly events

/etc/systemd/system/timer-hourly.timer
[Unit]
Description=Hourly Timer

[Timer]
OnBootSec=5min
OnUnitActiveSec=1h
Unit=timer-hourly.target

[Install]
WantedBy=basic.target
/etc/systemd/system/timer-hourly.target
[Unit]
Description=Hourly Timer Target
StopWhenUnneeded=yes

Daily events

/etc/systemd/system/timer-daily.timer
[Unit]
Description=Daily Timer

[Timer]
OnBootSec=10min
OnUnitActiveSec=1d
Unit=timer-daily.target

[Install]
WantedBy=basic.target
/etc/systemd/system/timer-daily.target
[Unit]
Description=Daily Timer Target
StopWhenUnneeded=yes

Systemd does not currently reschedule timers based on the last time they were run if the system is rebooted in between, i.e. it does not implement an anacron replacement. However, anacron can be used in conjunction with Systemd by updating the anacron timestamps through an hourly Systemd timer.

Weekly events

/etc/systemd/system/timer-weekly.timer
[Unit]
Description=Weekly Timer

[Timer]
OnBootSec=15min
OnCalendar=weekly
Unit=timer-weekly.target

[Install]
WantedBy=basic.target
/etc/systemd/system/timer-weekly.target
[Unit]
Description=Weekly Timer Target
StopWhenUnneeded=yes

Monthly events

/etc/systemd/system/timer-monthly.timer
[Unit]
Description=Monthly Timer

[Timer]
OnBootSec=45min
OnCalendar=monthly
Unit=timer-monthly.target

[Install]
WantedBy=basic.target
/etc/systemd/system/timer-monthly.target
[Unit]
Description=Monthly Timer Target
StopWhenUnneeded=yes

Yearly events

/etc/systemd/system/timer-yearly.timer
[Unit]
Description=Yearly Timer

[Timer]
OnBootSec=90min
OnCalendar=yearly
Unit=timer-yearly.target

[Install]
WantedBy=basic.target
/etc/systemd/system/timer-yearly.target
[Unit]
Description=Yearly Timer Target
StopWhenUnneeded=yes

Adding events

Adding events to these targets is as easy as dropping them into the correct wants folder. So if you wish for a particular job to take place daily, create a systemd service file and drop it into the relevant folder.

For example, if you wish to run foo.service daily (which runs program bar), you would create the following file:

/etc/systemd/system/timer-daily.target.wants/foo.service
[Unit]
Description=Starts program bar

[Service]
User=                                          # Add a user if you wish the service to be executes as a particular user, else delete this line
Type=                                          # Simple by default, change it if you know what you are doing, else delete this line
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/usr/bin/bar --option1 --option2     # More than one ExecStart can be used if required

Some important default Arch Linux cronjobs

If you wish to completely migrate away from cron to systemd, then it is prudent to have these services up and running for your system to remain workable.

logrotate

This service runs logrotate, which rotates, compresses and mails system logs. It relies upon package logrotate which by default provides a cron job for this task (/etc/cron.daily/updatedb). See man logrotate for details.

/etc/systemd/system/timer-daily.target.wants/logrotate.service
[Unit]
Description=Rotate Logs

[Service]
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/usr/bin/logrotate /etc/logrotate.conf

man-db

This service runs mandb, which creates or updates the manual page index caches. It relies upon package man-db which by default provides a cron job for this task (/etc/cron.daily/man-db). See man mandb for details.

/etc/systemd/system/timer-daily.target.wants/man-db-update.service
[Unit]
Description=Update man-db

[Service]
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/usr/bin/mandb --quiet

shadow

This service runs pwck and grpck, which together verify integrity of password and group files (/etc/{passwd,shadow,group,gshadow} ). Those tools are provided by the shadow package, which by default provides a cron job for this task (/etc/cron.daily/shadow). See man shadow, man pwck and man grpck for details.

/etc/systemd/system/timer-daily.target.wants/verify-shadow.service
[Unit]
Description=Verify integrity of password and group files

[Service]
Type=oneshot
ExecStart=/usr/bin/pwck -r
ExecStart=/usr/bin/grpck -r

Enable and start the timers

# systemctl enable timer-{hourly,daily,weekly,monthly,yearly}.timer 
# systemctl start timer-{hourly,daily,weekly,monthly,yearly}.timer

Starting events according to the calendar

If you wish to start a service according to a calendar event and not a monotonic interval (i.e. you wish to replace the functionality of crontab), you will need to create a new timer and link your service file to that. An example would be:

/etc/systemd/system/foo.timer
[Unit]
Description=foo timer

[Timer]
OnCalendar=Mon-Thu *-9-28 *:30:00 # To add a time of your choosing here, please refer to systemd.time manual page for the correct format
Unit=foo.service

[Install]
WantedBy=basic.target

The service file may be created the same way as the events for monotonic clocks. However, take care to put them in the /etc/systemd/system/ folder.

Custom/example service files

Note: These example scripts assume that you have created the hourly, daily and weekly timers as described above.

Reflector

This service file may be used to update pacman's mirrorlist daily using the reflector script.

/etc/systemd/system/timer-daily.target.wants/reflector.service
[Unit]
Description=Update the mirrorlist

[Service]
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/usr/bin/reflector --protocol http --latest 30 --number 20 --sort rate --save /etc/pacman.d/mirrorlist
Note: Reflector's options should be tweaked according to users' criteria. Use case examples may be found in this forum post by Moderator/TU Xyne.

pkgstats

If you have pkgstats installed, this service will be necessary in order to send data back to the Arch servers.

Tip: It should be noted that the user nobody, which is being used here, is already present on all Arch systems. The use of this user is recommended for all services which can function in a completely unprivileged environment.
/etc/systemd/system/timer-weekly.target.wants/pkgstats.service
[Unit]
Description=Run pkgstats

[Service]
User=nobody
ExecStart=/usr/bin/pkgstats -q

Update mlocate database

This service runs updatedb, which updates the mlocate database. The package mlocate provides by default a cron job for this task (/etc/cron.daily/updatedb). See man 8 updatedb for details.

/etc/systemd/system/timer-daily.target.wants/mlocate-update.service
[Unit]
Description=Update mlocate database

[Service]
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/usr/bin/updatedb -f proc

The modprobed_db service

This service is of great use to people who compile their own kernels because it reduces compilation time by a significant amount. Refer to the Modprobed_db page for further details.

/etc/systemd/system/timer-daily.target.wants/modprobed_db.service
[Unit]
Description=Run modprobed_db

[Service]
User=enter user here
ExecStart=/usr/bin/modprobed_db store

The hosts-update service

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: missing package requirements, is it hosts-updateAUR? (Discuss in Talk:Systemd/Timers#)

Updates /etc/hosts with the mvps blocklist.

/etc/systemd/system/timer-daily.target.wants/hosts-update.service
[Unit]
Description=Update hosts file
After=network.target

[Service]
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/usr/bin/hosts-update

[Install]
WantedBy=timer-daily.target

See also