|
The
cron time-based job scheduler
The cron program is used to
schedule commands (Cron Jobs) at a specified time. There is a
cron and a crontab program. Crontab is the program used to
install, deinstall or list the crontab table used to drive the
cron program.
To run cron, you can enter it on the command
line or add it to the "/etc/rc.sys" file to
have it started when ELKS boots. The etc directory can be found
in "elkscmd/rootfs_template/etc/". Edit the rc.sys file
in there and add cron. Then the disk image will be generated
with the modified file. Cron will scan the directory
/var/cron for crontab files and execute these if there
are any or they have been modified. If you logged in as root the
name of your crontab file will be root. Cron runs as a daemon in
the background and therefore will not output anything to the
console screen. If you want to save the output of the executed
program you have to add e.g. „>> cron.out“ to
the command in the crontab file.
The ELKS version of cron
will not send an email when the scheduled job is completed but
make an entry in the /var/cron/cron.log file.
If you
run ELKS on qemu, the hardware time will be UTC or Greenich mean
time. Therefore the jobs will not be executed at the local time
if you do not set the ELKS time to your local time. If you
schedule a lot of jobs you may run out of task slots.
To display the contents
of the crontab file call the crontab program with the "-l"
parameter:
$ crontab
-l
To edit the cron jobs, do:
$
crontab -e
crontab defaults to the vi editor in this
case and opens the crontab file in the vi editor. To set it to
the kilo editor, set the environment variable $EDITOR to
kilo.
Each line in the crontab file defines a different
command or cron job which can be executed at different times.
An example of such a line would be:
*/5
* * * * ls -l >>/var/cron/cron.log
This
runs "ls -l", which is the cron job, at every 5th
minute. For example if the time is 10:00, the next job will
run at 10:05, 10:10, 10:15 and so on.
The five stars before
the command are defined as follows:
#
.---------------- minute (0 - 59) # | .------------- hour (0 -
23) # | | .---------- day of month (1 - 31) # | | | .-------
month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of
week (0 - 6) (Sunday=0 or 7) OR sun, # | | | |
| mon,tue,wed,thu,fri,sat # * * *
* * command to be executed
Here are examples of cron
jobs which can be defined in the crontab file:
1. To run a
cron job at every minute, the format should be like below.
*
* * * * <command-to-execute>
For example if
the time is 10:00, the next job will run at 10:01, 10:02, 10:03
and so on.
2. To run a cron job at every quarter hour (i.e
every 15th minute), add this:
*/15
* * * * <command-to-execute>
For example if
the time is 10:00, the next job will run at 10:15, 10:30, 10:45
and so on.
3. To run a cron job every hour at minute
30:
30 * * * *
<command-to-execute>
For example if the time
is 10:00, the next job will run at 10:30, 11:30, 12:30 and so
on.
4. You can also define multiple time intervals
separated by commas. For example, the following cron job will run
three times every hour, at minute 0, 5 and 10:
0,5,10
* * * * <command-to-execute>
5. Run a job
every hour (at minute 0):
0
* * * * <command-to-execute>
For example if
the time is now 10:00, the next job will run at 11:00, 12:00,
12:00 and so on.
6. Run a job every 2 hours:
0
*/2 * * * <command-to-execute>
For example if
the time is now 10:00, the next job will run at 12:00.
7.
Run a job every day at 3am:
0
3 * * * <command-to-execute>
8. Run a job
every Sunday:
0 0 * *
sun <command-to-execute>
9. Run a job on every
day-of-week from Monday through Friday i.e every weekday:
0
0 * * 1-5 <command-to-execute>
The job will
start at 00:00.
10. Run a job at 16:15 on day-of-month
1:
15 16 1 * *
<command-to-execute>
In the ELKS version
you cannot use % or newline to add input for the command.
9th
of May 2020 Georg Potthast
|