Cron: Difference between revisions

From Alpine Linux
(Added information and example for dcron)
(added verification steps from Setting up ZFS with native encryption page)
 
(5 intermediate revisions by 2 users not shown)
Line 2: Line 2:
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}}.
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.
== Basic example ==


Examples of how to achieve this in different <code>crond</code> implementations can be found below.
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 {{Path|/etc/periodic/daily}} folder or use another folder under the {{Path|/etc/periodic/}} hierarchy if you want to run the script more/less frequently.
 
Create a script file {{Path|/etc/periodic/daily/do-ntp}} as follows:{{Cat|/etc/periodic/daily/do-ntp|<nowiki>#!/bin/sh
ntpd -d -q -n -p uk.pool.ntp.org</nowiki>}}
Make the script file executable:{{Cmd|# 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 [https://www.pool.ntp.org/zone/@ more info].
 
To check whether any {{ic|cron}} service is running, verify the output for the command : {{cmd|<nowiki># rc-update |grep cron
crond |      default </nowiki>}}
 
== Busybox crond ==
 
{{ic|crond}} is the builtin cron implementation from [[BusyBox]].
 
=== Configuration ===
 
The {{ic|crond}} service from [[BusyBox]] is managed by [[OpenRC]].
 
To immediately start, stop and restart the {{ic|crond}} service: {{cmd|# rc-service crond start}}
 
To add {{ic|crond}} service to the boot sequence: {{cmd|# rc-update add crond}}
 
{{Note|Whenever an alternative cron implementation is used, ensure that the {{ic|crond}} service is stopped and removed from [[OpenRC]] as follows:{{Cmd|<nowiki># rc-service crond stop
# rc-update del crond </nowiki>}} }}
 
== 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 <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.  


== Cronie ==
== Cronie ==


The {{Pkg|cronie}} package comes with <code>anacron</Code> tool, which does this kind of asynchronous job processing.  
The {{Pkg|cronie}} package comes with <code>anacron</Code> tool for [[#Asynchronous job processing|asynchronous job processing]]. To use it, first install the {{Pkg|cronie}} package: {{Cmd|# apk add {{Pkg|cronie}}}}
 
Finally, start {{Pkg|cronie}} and tell [[OpenRC]] to start it at boot:{{Cmd|<nowiki>
# rc-service cronie start
# rc-update add cronie
</nowiki>}}


=== Anacron ===
=== Configuration ===


Here are the steps to use <code>anacron</Code> in Alpine Linux:
Here are the steps to use <code>anacron</Code> tool from cronie in Alpine Linux.


# Add the line {{Codeline|<code>@reboot /usr/sbin/anacron -s</code>}} to the crontab of the '''root''' user:
# Add the line {{Codeline|<code>@reboot /usr/sbin/anacron -s</code>}} to the crontab of the '''root''' user:
Line 29: Line 62:
# Anacron needs the folder {{Path|/var/spool/anacron}} to avoid the error {{Codeline|cron.err anacron[2893]: Can't chdir to /var/spool/anacron: No such file or directory}}. So create the folder using the command:
# Anacron needs the folder {{Path|/var/spool/anacron}} to avoid the error {{Codeline|cron.err anacron[2893]: Can't chdir to /var/spool/anacron: No such file or directory}}. So create the folder using the command:
#: {{Cmd|# mkdir /var/spool/anacron}}
#: {{Cmd|# mkdir /var/spool/anacron}}
# Edit the configuration file {{Path|/etc/anacrontab}} and test the configuration validity by <Code>anacron -T</Code>.
# Edit the configuration file {{Path|/etc/anacrontab}} where jobs are listed as per the above crontab syntax, 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>.
For more information about {{Pkg|cronie}}'s options and its <code>anacrontab</code> format, check out the <code>anacrontab(5)</code>, <code>anacron(8)</code> and <code>crond(8)</code> manpages, which you can install with the {{Pkg|cronie-doc}} package.


== dcron ==
== 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:
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:{{Cmd|# apk add dcron}}
 
# 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:


Finally, start {{Pkg|dcron}} and tell [[OpenRC]] to start it at boot:{{Cmd|<nowiki>
  # rc-service dcron start
  # rc-service dcron start
  # rc-update add dcron
  # rc-update add dcron
</nowiki>}}


=== Configuration ===
=== Configuration ===
Line 52: Line 79:
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
Line 64: Line 91:
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.
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://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://www.jimpryor.net/linux/dcron.html Official dcron website]
* [https://crontab.guru/ crontab Generator]
* [https://crontab.guru/ crontab Generator]
 
* [https://wiki.archlinux.org/title/Cron Archwiki Cron page]
[[Category:System Administration‏‎ ]]
[[Category:System Administration‏‎ ]]
[[Category:Desktop]]
[[Category:Desktop]]

Latest revision as of 09:45, 2 April 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.

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.

To check whether any cron service is running, verify the output for the command :

# rc-update |grep cron crond | default

Busybox crond

crond is the builtin cron implementation from BusyBox.

Configuration

The crond service from BusyBox is managed by OpenRC.

To immediately start, stop and restart the crond service:

# rc-service crond start

To add crond service to the boot sequence:

# rc-update add crond

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

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

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.

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