Cron: Difference between revisions
Prabuanand (talk | contribs) (added instruction about making the file executable) |
Prabuanand (talk | contribs) (added verification steps from Setting up ZFS with native encryption page) |
||
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}} 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}}. | ||
== Basic example == | == Basic example == | ||
Line 14: | Line 11: | ||
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]. | 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 == | == 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 | 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 == |
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
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
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.
- 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 where jobs are listed as per the above crontab syntax, 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
.
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
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.