Cron: Difference between revisions
Prabuanand (talk | contribs) m (added command needed to view) |
(Added information and example for dcron) |
||
Line 1: | Line 1: | ||
This page documents the working of [https://en.wikipedia.org/wiki/Cron Cron], a job scheduler on Unix-like operating systems. Cron is most suitable for scheduling repetitive tasks. Scheduling one-time tasks can be accomplished using the associated at utility. | This page documents the working of [https://en.wikipedia.org/wiki/Cron Cron], a job scheduler on Unix-like operating systems. Cron is most suitable for scheduling repetitive tasks. Scheduling one-time tasks can be accomplished using the associated at utility. | ||
There are many cron implementations, but Alpine Linux comes inbuilt with the [[BusyBox]] version of cron. Some of the other packages available in Alpine Linux are {{Pkg|cronie}}, {{Pkg|fcron}} | There are many cron implementations, but Alpine Linux comes inbuilt with the [[BusyBox]] version of cron. Some of the other packages available in Alpine Linux are {{Pkg|cronie}}, {{Pkg|fcron}} and {{Pkg|dcron}}. | ||
For systems that do not run continuously, like laptops or regular desktop computers, cron jobs with large time intervals between invocations may not be executed at all if the system is suspended or powered off during the cron job's scheduled time. To work around this, many <code>crond</code> implementations provide special directives like <code>@daily</code> and <code>@monthly</code> which keep track of the last execution time and still execute if the system was down while the job was supposed to run. | |||
Examples of how to achieve this in different <code>crond</code> implementations can be found below. | |||
== Cronie == | == Cronie == | ||
The {{Pkg|cronie}} package comes with <code>anacron</Code> tool, which does this kind of asynchronous job processing. | |||
=== Anacron === | === Anacron === | ||
Line 27: | Line 31: | ||
# Edit the configuration file {{Path|/etc/anacrontab}} and test the configuration validity by <Code>anacron -T</Code>. | # Edit the configuration file {{Path|/etc/anacrontab}} and test the configuration validity by <Code>anacron -T</Code>. | ||
# Reboot the computer to test the working of anacron. The syslog file {{Path|/var/log/messages}} captures messages from anacron and can be searched by <Code>cat /var/log/messages |grep anacron</Code>. | # Reboot the computer to test the working of anacron. The syslog file {{Path|/var/log/messages}} captures messages from anacron and can be searched by <Code>cat /var/log/messages |grep anacron</Code>. | ||
== dcron == | |||
Another available crond that fully integrates the features of <code>anacron</code> is {{Pkg|dcron}}, "dillon's lightweight cron daemon". To use it, first install the {{Pkg|dcron}} package: | |||
# apk add dcron | |||
Next, make sure that the default <code>crond</code> from [[BusyBox]] is stopped and removed from OpenRC: | |||
# rc-service crond stop | |||
# rc-update del crond | |||
Finally, start {{Pkg|dcron}} and tell OpenRC to start it at boot: | |||
# rc-service dcron start | |||
# rc-update add dcron | |||
=== Configuration === | |||
You can add directives to the crontab with the <code>crontab -e</code> command, also including the <code>@hourly</code>, <code>@daily</code>, <code>@weekly</code>, <code>@monthly</code> and <code>@yearly</code> directives. | |||
The syntax of these special directives differs slightly differs from <code>anacron</code>. Here is how Alpine's default could be adjusted to use the {{Pkg|dcron}} syntax with the special directives to ensure that they are executed even with systems that are not always running: | |||
{{Cat|/var/spool/cron/crontabs/root|<nowiki># do daily/weekly/monthly maintenance | |||
# min hour day month weekday command | |||
*/15 * * * * run-parts /etc/periodic/15min | |||
@hourly ID=periodic.hourly run-parts /etc/periodic/hourly | |||
@daily ID=periodic.daily run-parts /etc/periodic/daily | |||
@weekly ID=periodic.weekly run-parts /etc/periodic/weekly | |||
@monthly ID=periodic.monthly run-parts /etc/periodic/monthly</nowiki>}} | |||
As you can see, the name or "ID" of a job is set in the style of a shell variable assignment. For more information about {{Pkg|dcron}}'s options and its <code>crontab</code> format, check out the <code>crontab(1)</code> and <code>crond(8)</code> manpages, which you can install with the {{Pkg|dcron-doc}} package. | |||
== See Also == | == See Also == | ||
* [https://wiki.archlinux.org/title/Cron Archwiki Cron page] | * [https://wiki.archlinux.org/title/Cron Archwiki Cron page] | ||
* [https://github.com/cronie-crond/cronie Official cronie website] | * [https://github.com/cronie-crond/cronie Official cronie website] | ||
* [https://www.jimpryor.net/linux/dcron.html Official dcron website] | |||
* [https://crontab.guru/ crontab Generator] | * [https://crontab.guru/ crontab Generator] | ||
[[Category:System Administration ]] | [[Category:System Administration ]] | ||
[[Category:Desktop]] | [[Category:Desktop]] |
Revision as of 20:06, 14 February 2025
This page documents the working of Cron, a job scheduler on Unix-like operating systems. Cron is most suitable for scheduling repetitive tasks. Scheduling one-time tasks can be accomplished using the associated at utility. There are many cron implementations, but Alpine Linux comes inbuilt with the BusyBox version of cron. Some of the other packages available in Alpine Linux are cronie, fcron and dcron.
For systems that do not run continuously, like laptops or regular desktop computers, cron jobs with large time intervals between invocations may not be executed at all if the system is suspended or powered off during the cron job's scheduled time. To work around this, many crond
implementations provide special directives like @daily
and @monthly
which keep track of the last execution time and still execute if the system was down while the job was supposed to run.
Examples of how to achieve this in different crond
implementations can be found below.
Cronie
The cronie package comes with anacron
tool, which does this kind of asynchronous job processing.
Anacron
Here are the steps to use anacron
in Alpine Linux:
- Add the line
@reboot /usr/sbin/anacron -s
to the crontab of the root user:# crontab -e
- Once edited, root crontab should appear as follows, when viewed using the command
# crontab -l
or viewed directly:Contents of /var/spool/cron/crontabs/root
# do daily/weekly/monthly maintenance # min hour day month weekday command */15 * * * * run-parts /etc/periodic/15min 0 * * * * run-parts /etc/periodic/hourly 0 2 * * * run-parts /etc/periodic/daily 0 3 * * 6 run-parts /etc/periodic/weekly 0 5 1 * * run-parts /etc/periodic/monthly @reboot /usr/sbin/anacron -s
- Anacron needs the folder /var/spool/anacron to avoid the error cron.err anacron[2893]: Can't chdir to /var/spool/anacron: No such file or directory. So create the folder using the command:
# mkdir /var/spool/anacron
- Edit the configuration file /etc/anacrontab and test the configuration validity by
anacron -T
. - Reboot the computer to test the working of anacron. The syslog file /var/log/messages captures messages from anacron and can be searched by
cat /var/log/messages |grep anacron
.
dcron
Another available crond that fully integrates the features of anacron
is dcron, "dillon's lightweight cron daemon". To use it, first install the dcron package:
# apk add dcron
Next, make sure that the default crond
from BusyBox is stopped and removed from OpenRC:
# rc-service crond stop # rc-update del crond
Finally, start dcron and tell OpenRC to start it at boot:
# rc-service dcron start # rc-update add dcron
Configuration
You can add directives to the crontab with the crontab -e
command, also including the @hourly
, @daily
, @weekly
, @monthly
and @yearly
directives.
The syntax of these special directives differs slightly differs from anacron
. Here is how Alpine's default could be adjusted to use the dcron syntax with the special directives to ensure that they are executed even with systems that are not always running:
Contents of /var/spool/cron/crontabs/root
As you can see, the name or "ID" of a job is set in the style of a shell variable assignment. For more information about dcron's options and its crontab
format, check out the crontab(1)
and crond(8)
manpages, which you can install with the dcron-doc package.