Cron: Difference between revisions

From Alpine Linux
(Added information and example for dcron)
mNo edit summary
Line 52: Line 52:
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.
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:
The syntax of these special directives 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
{{Cat|/var/spool/cron/crontabs/root|<nowiki># do daily/weekly/monthly maintenance

Revision as of 20:09, 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:

  1. Add the line @reboot /usr/sbin/anacron -s to the crontab of the root user:

    # crontab -e

  2. 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
  3. 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

  4. Edit the configuration file /etc/anacrontab and test the configuration validity by anacron -T.
  5. 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 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

# 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

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.

See Also