Difference between revisions of "Systemd/Timers"

From ArchWiki
Jump to: navigation, search
m (Enable and start the timers: Shortened many commands into one)
m (See also: Edited for grammar)
(4 intermediate revisions by 2 users not shown)
Line 16: Line 16:
 
== Introduction ==
 
== 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:
+
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
 
* 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 ot 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.  
+
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 and weekly events ==
 
== Hourly, daily and weekly events ==
Line 28: Line 28:
 
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].
  
{{Note|There is an AUR package {{aur|systemd-cron}} 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.}}
+
{{Note|There is an AUR package {{AUR|systemd-cron}} 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:
 
First, the creation of a few directories is required:
Line 36: Line 36:
 
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.
  
=== Hourly Events ===
+
=== Hourly events ===
  
{{hc|/etc/systemd/system/timer-hourly.timer|
+
{{hc|/etc/systemd/system/timer-hourly.timer|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Hourly Timer
 
Description=Hourly Timer
  
Line 48: Line 48:
  
 
[Install]
 
[Install]
WantedBy=basic.target</nowiki>}}
+
WantedBy=basic.target
 +
</nowiki>}}
  
{{hc|/etc/systemd/system/timer-hourly.target|
+
{{hc|/etc/systemd/system/timer-hourly.target|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Hourly Timer Target
 
Description=Hourly Timer Target
StopWhenUnneeded=yes</nowiki>}}
+
StopWhenUnneeded=yes
 +
</nowiki>}}
  
=== Daily Events ===
+
=== Daily events ===
  
{{hc|/etc/systemd/system/timer-daily.timer|
+
{{hc|/etc/systemd/system/timer-daily.timer|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Daily Timer
 
Description=Daily Timer
  
Line 67: Line 69:
  
 
[Install]
 
[Install]
WantedBy=basic.target</nowiki>}}
+
WantedBy=basic.target
 +
</nowiki>}}
  
{{hc|/etc/systemd/system/timer-daily.target|
+
{{hc|/etc/systemd/system/timer-daily.target|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Daily Timer Target
 
Description=Daily Timer Target
StopWhenUnneeded=yes</nowiki>}}
+
StopWhenUnneeded=yes
 +
</nowiki>}}
  
=== Weekly Events ===
+
=== Weekly events ===
  
{{hc|/etc/systemd/system/timer-weekly.timer|
+
{{hc|/etc/systemd/system/timer-weekly.timer|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Weekly Timer
 
Description=Weekly Timer
  
Line 86: Line 90:
  
 
[Install]
 
[Install]
WantedBy=basic.target</nowiki>}}
+
WantedBy=basic.target
 +
</nowiki>}}
  
{{hc|/etc/systemd/system/timer-weekly.target|
+
{{hc|/etc/systemd/system/timer-weekly.target|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Weekly Timer Target
 
Description=Weekly Timer Target
StopWhenUnneeded=yes</nowiki>}}
+
StopWhenUnneeded=yes
 +
</nowiki>}}
  
 
=== Adding events ===
 
=== Adding events ===
Line 99: Line 105:
 
For example, if you wish to run {{ic|foo.service}} daily (which runs program bar), you would create the following file:
 
For example, if you wish to run {{ic|foo.service}} daily (which runs program bar), you would create the following file:
  
{{hc|/etc/systemd/system/timer-daily.target.wants/foo.service|
+
{{hc|/etc/systemd/system/timer-daily.target.wants/foo.service|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Starts program bar
 
Description=Starts program bar
  
Line 109: Line 115:
 
IOSchedulingClass=2
 
IOSchedulingClass=2
 
IOSchedulingPriority=7
 
IOSchedulingPriority=7
ExecStart=/usr/bin/bar --option1 --option2    # More than one ExecStart can be used if required</nowiki>}}
+
ExecStart=/usr/bin/bar --option1 --option2    # More than one ExecStart can be used if required
 +
</nowiki>}}
  
 
=== Some important default Arch Linux cronjobs ===
 
=== 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.
+
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 ====
  
{{hc|/etc/systemd/system/timer-daily.target.wants/logrotate.service|
+
{{hc|/etc/systemd/system/timer-daily.target.wants/logrotate.service|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Rotate Logs
 
Description=Rotate Logs
  
Line 125: Line 132:
 
IOSchedulingClass=2
 
IOSchedulingClass=2
 
IOSchedulingPriority=7
 
IOSchedulingPriority=7
ExecStart=/usr/bin/logrotate /etc/logrotate.conf</nowiki>}}
+
ExecStart=/usr/bin/logrotate /etc/logrotate.conf
 +
</nowiki>}}
  
 
==== Update man-db ====
 
==== Update man-db ====
  
{{hc|/etc/systemd/system/timer-daily.target.wants/man-db-update.service|
+
{{hc|/etc/systemd/system/timer-daily.target.wants/man-db-update.service|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Update man-db
 
Description=Update man-db
  
Line 137: Line 145:
 
IOSchedulingClass=2
 
IOSchedulingClass=2
 
IOSchedulingPriority=7
 
IOSchedulingPriority=7
ExecStart=/usr/bin/mandb --quiet</nowiki>}}
+
ExecStart=/usr/bin/mandb --quiet
 +
</nowiki>}}
  
 
==== Update mlocate database ====
 
==== Update mlocate database ====
  
{{hc|/etc/systemd/system/timer-daily.target.wants/mlocate-update.service|
+
{{hc|/etc/systemd/system/timer-daily.target.wants/mlocate-update.service|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Update mlocate database
 
Description=Update mlocate database
  
Line 149: Line 158:
 
IOSchedulingClass=2
 
IOSchedulingClass=2
 
IOSchedulingPriority=7
 
IOSchedulingPriority=7
ExecStart=/usr/bin/updatedb</nowiki>}}
+
ExecStart=/usr/bin/updatedb
 +
</nowiki>}}
  
 
==== Verify integrity of password and group files ====
 
==== Verify integrity of password and group files ====
  
{{hc|/etc/systemd/system/timer-daily.target.wants/verify-shadow.service|
+
{{hc|/etc/systemd/system/timer-daily.target.wants/verify-shadow.service|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Verify integrity of password and group files
 
Description=Verify integrity of password and group files
  
Line 160: Line 170:
 
Type=oneshot
 
Type=oneshot
 
ExecStart=/usr/bin/pwck -r
 
ExecStart=/usr/bin/pwck -r
ExecStart=/usr/bin/grpck -r</nowiki>}}
+
ExecStart=/usr/bin/grpck -r
 +
</nowiki>}}
  
 
=== Enable and start the timers ===
 
=== Enable and start the timers ===
Line 170: Line 181:
 
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:
 
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:
  
{{hc|/etc/systemd/system/foo.timer|
+
{{hc|/etc/systemd/system/foo.timer|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=foo timer
 
Description=foo timer
  
 
[Timer]
 
[Timer]
OnCalendar=Mon-Thu *-9-28 *:30:00 # To add a time of your choosing here, please refer to systemd.time man page for the correct format
+
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
 
Unit=foo.service
  
 
[Install]
 
[Install]
WantedBy=basic.target</nowiki>}}
+
WantedBy=basic.target
 +
</nowiki>}}
  
 
The service file may be created the same way as the events for monotonic clocks. However, take care to put them in the {{ic|/etc/systemd/system/}} folder.
 
The service file may be created the same way as the events for monotonic clocks. However, take care to put them in the {{ic|/etc/systemd/system/}} folder.
  
== Custom/Example service files ==
+
== Custom/example service files ==
  
 
{{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.}}
Line 189: Line 201:
 
=== The pkgstats service ===
 
=== The pkgstats service ===
  
If you have the {{pkg|pkgstats}} package installed, this service will be necessary in order to send data back to the arch servers.
+
If you have the {{Pkg|pkgstats}} package 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.}}
+
{{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|
+
{{hc|/etc/systemd/system/timer-weekly.target.wants/pkgstats.service|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Run pkgstats
 
Description=Run pkgstats
  
 
[Service]
 
[Service]
 
User=nobody
 
User=nobody
ExecStart=/usr/bin/pkgstats</nowiki>}}
+
ExecStart=/usr/bin/pkgstats
 +
</nowiki>}}
  
 
=== The modprobed_db service ===
 
=== The modprobed_db service ===
Line 205: Line 218:
 
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.
 
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/modprobed_db.service|
+
{{hc|/etc/systemd/system/timer-daily.target.wants/modprobed_db.service|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Run modprobed_db
 
Description=Run modprobed_db
  
 
[Service]
 
[Service]
User=<enter user here>
+
User=</nowiki>''enter user here''<nowiki>
ExecStart=/usr/bin/modprobed_db store</nowiki>}}
+
ExecStart=/usr/bin/modprobed_db store
 +
</nowiki>}}
  
 
=== The reflector service ===
 
=== The reflector service ===
  
This service file may be used to upgrade the mirrorlist daily using the [[reflector]] program.
+
This service file may be used to upgrade the mirrorlist daily using the [[Reflector]] program.
  
{{hc|/etc/systemd/system/timer-daily.target.wants/reflector.service|
+
{{hc|/etc/systemd/system/timer-daily.target.wants/reflector.service|<nowiki>
<nowiki>[Unit]
+
[Unit]
 
Description=Update the mirrorlist
 
Description=Update the mirrorlist
  
Line 225: Line 239:
 
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</nowiki>}}
+
ExecStart=/usr/bin/reflector --protocol http --latest 30 --number 20 --sort rate --save /etc/pacman.d/mirrorlist
 +
</nowiki>}}
  
== External Links ==
+
=== The man-db service ===
* [https://fedoraproject.org/wiki/Features/SystemdCalendarTimers Systemd Calendar Timers on the Fedora Project wiki]
+
 
 +
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}}).
 +
 
 +
{{hc|/etc/systemd/system/timer-daily.target.wants/man-db.service|<nowiki>
 +
[Unit]
 +
Description=Update manual page index caches
 +
 
 +
[Service]
 +
Nice=19
 +
IOSchedulingClass=2
 +
IOSchedulingPriority=7
 +
ExecStart=/usr/bin/mandb --quiet
 +
</nowiki>}}
 +
 
 +
=== The shadow 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}}).
 +
 
 +
{{hc|/etc/systemd/system/timer-daily.target.wants/shadow.service|<nowiki>
 +
[Unit]
 +
Description=Verify integrity of password and group files
 +
 
 +
[Service]
 +
Type=oneshot
 +
ExecStart=/usr/bin/pwck -r
 +
ExecStart=/usr/bin/grpck -r
 +
</nowiki>}}
 +
 
 +
== See also ==
 +
 
 +
* https://fedoraproject.org/wiki/Features/SystemdCalendarTimers - systemd calendar timers on the Fedora Project wiki

Revision as of 20:53, 5 October 2013

Summary help replacing me
Covers how to replace cron by systemd.
Related
systemd
systemd/User
systemd/Services
systemd FAQ
cron

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 ot be done properly (eg IOSchedulingPriority, Nice or JobTimeoutSec)
  • 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 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 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}.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

Weekly events

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

[Timer]
OnBootSec=15min
OnUnitActiveSec=1w
Unit=timer-weekly.target

[Install]
WantedBy=basic.target
/etc/systemd/system/timer-weekly.target
[Unit]
Description=Weekly 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

/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

Update man-db

/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

Update mlocate database

/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

Verify integrity of password and group files

/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}.timer && systemctl start timer-{hourly,daily,weekly}.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.

The pkgstats service

If you have the pkgstats package 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

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 reflector service

This service file may be used to upgrade the mirrorlist daily using the Reflector program.

/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

The man-db service

This service runs mandb, which updates the manual page index caches. See man 8 mandb for details. The package man-db provides a cron job for this task (file /etc/cron.daily/man-db).

/etc/systemd/system/timer-daily.target.wants/man-db.service
[Unit]
Description=Update manual page index caches

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

The shadow service

This service verifies integrity of password and group files (/etc/{passwd,shadow,group,gshadow} ). The package shadow provides a cron job for this task (file /etc/cron.daily/shadow).

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

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

See also