Cron

From Alpine Linux

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.

Note: Whenever an alternative cron implementation is used, ensure that the default crond from BusyBox is stopped and removed from OpenRC as follows:

# rc-service crond stop # rc-update del crond

Basic example

The below example uses a cron job to keep the time in sync. This example works with all versions of cron including the BusyBox version of cron. Add the following file or a symlink to /etc/periodic/daily folder or use another folder under the /etc/periodic/ hierarchy if you want to run the script more/less frequently.

Create a script file /etc/periodic/daily/do-ntp as follows:

Contents of /etc/periodic/daily/do-ntp

#!/bin/sh ntpd -d -q -n -p uk.pool.ntp.org

Make the script file executable:

# chmod +x /etc/periodic/daily/do-ntp

This script queries the uk time server pool - modify this to suit your location, or just use pool.ntp.org. Refer for more info.

Asynchronous job processing

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 in this page.

Cronie

The cronie package comes with anacron tool for asynchronous job processing. To use it, first install the cronie package:

# apk add cronie

Finally, start cronie and tell OpenRC to start it at boot:

# rc-service cronie start # rc-update add cronie

Configuration

Here are the steps to use anacron tool from cronie 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 where jobs are listed as per the above crontab syntax, 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.

For more information about cronie's options and its anacrontab format, check out the anacrontab(5), anacron(8) and crond(8) manpages, which you can install with the cronie-doc package.

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

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