In Linux there are several ways to execute automatic tasks: to run periodical tasks use cron command, to run one-time tasks on a specified date use the at command and to run one-time tasks when the system load average is below a specified number use the batch command.
Cron
The crond service is used to schedule periodic tasks and is installed with the 'vixie-cron' rpm. The cron system-wide configuration file is /etc/crontab :
The first four line are the environment variables configuration used in cron execution. The following lines are periodic executions with the format :
minute hour day month day-of-week command
minute : (0-59)
hour : (0-23)
day : (1-31)
month : (1-12) or (jan-dec)
dayofweek : (0-7) or ( sun-sat)
command : command/script to execute
For any of these values an '*' means all possible values, for example an '*' on the hour value means at any hour. An '-' means a range, for example '0-2' on the day value means 0,1,2 (from Sunday to Tuesday) the same as a list separated by ',' that means a list of values 0,1,2. The forward slash '/' means a step value, for example '*/2' on the hour field means every two hours 0,2,4,6,..,22.
The '/etc/crontab' files executes as root the commands/scripts on /etc/cron.hourly every hour, /etc/cron.daily every day at 04:02, /etc/cron.weekly every Sunday at 04:22 and /etc/cron.monthly every first of month at 04:42. If some task need to be scheduled at other time by other user the cron command can be used, for example if user 'john' needs to execute the script /home/john/script.sh every day at 05:00 :
The user-defined crontabs are stored in the /var/spool/cron/ directory and are executed with the owner´s file user ID by crond daemon which every minute checks in /etc/crontab file, /etc/cron.d and /var/spool/cron to see if there are any cron jobs to be executed. With the files /etc/cron.allow and /etc/cron.deny the root user can control which users can use the cron utility, by default all users can use cron.
Cron examples
12 0 * * * /bin/job1 >> /tmp/out 2>&1
# run /bin/job1 at 00:12 every night and redirect all OUTPUT to /tmp/out
10 18 2 * * /sbin/job2
# run /sbin/job2 at 18:10 on the second of every month
23 0-23/2 * * 1 /bin/job3
# run /bin/job3 at 00:23, 02:23, 04:23,...22:23 on Monday of every month
At
The atd service, installed by 'at' rpm, is responsible to schedule one-time task at specific time. The 'at' jobs are scheduled executing the at command as an specific user (that can be root or any other) that determines the user ID under the task will be executed. The files /etc/at.allow and /etc/at.deny can be used to determine which users can use 'at', by default all users can use it.
At examples
In 10 minutes 'echo 123' will be executed as user 'john'
In 10 hours /home/john/test.sh will be executed as user 'john'
At 2010-12-30 01:00 the command /sbin/reboot will be executed as root
Lists all atq scheduled tasks
Removes a specific at task
Batch
The batch command can be used to execute on-time tasks when the load system average is under 0.8. It uses the atd service so it is installed with 'at' rpm package. By default any user can use 'batch' command and the files /etc/at.allow and /etc/at.deny can be used to restrict which users are allowed to use batch.
Batch examples
Run 'echo 123' as user john when the system load average is under 0.8. Commands atq and atrm allows manipulate batch scheduling.
Cron
The crond service is used to schedule periodic tasks and is installed with the 'vixie-cron' rpm. The cron system-wide configuration file is /etc/crontab :
Code:
$ cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly
minute hour day month day-of-week command
minute : (0-59)
hour : (0-23)
day : (1-31)
month : (1-12) or (jan-dec)
dayofweek : (0-7) or ( sun-sat)
command : command/script to execute
For any of these values an '*' means all possible values, for example an '*' on the hour value means at any hour. An '-' means a range, for example '0-2' on the day value means 0,1,2 (from Sunday to Tuesday) the same as a list separated by ',' that means a list of values 0,1,2. The forward slash '/' means a step value, for example '*/2' on the hour field means every two hours 0,2,4,6,..,22.
The '/etc/crontab' files executes as root the commands/scripts on /etc/cron.hourly every hour, /etc/cron.daily every day at 04:02, /etc/cron.weekly every Sunday at 04:22 and /etc/cron.monthly every first of month at 04:42. If some task need to be scheduled at other time by other user the cron command can be used, for example if user 'john' needs to execute the script /home/john/script.sh every day at 05:00 :
Code:
su - john john-$ crontab -e 00 05 * * * /home/john/script.sh :wq! no crontab for john - using an empty one crontab: installing new crontab john-$ crontab -l 00 05 * * * /home/john/script.sh
Cron examples
12 0 * * * /bin/job1 >> /tmp/out 2>&1
# run /bin/job1 at 00:12 every night and redirect all OUTPUT to /tmp/out
10 18 2 * * /sbin/job2
# run /sbin/job2 at 18:10 on the second of every month
23 0-23/2 * * 1 /bin/job3
# run /bin/job3 at 00:23, 02:23, 04:23,...22:23 on Monday of every month
At
The atd service, installed by 'at' rpm, is responsible to schedule one-time task at specific time. The 'at' jobs are scheduled executing the at command as an specific user (that can be root or any other) that determines the user ID under the task will be executed. The files /etc/at.allow and /etc/at.deny can be used to determine which users can use 'at', by default all users can use it.
At examples
Code:
$ su - john john-$ at now + 10 minutes at> echo 123 at> Ctrl + d job 5 at 2010-10-30 12:47
Code:
john-$ at now + 10 hours at> /home/john/test.sh at> Ctrl + d job 6 at 2010-10-30 22:39
Code:
john-$ su - root $ at 01:00 12/30/2010 at> /sbin/reboot at> Ctrl + d job 7 at 2010-12-30 01:00
Code:
$ atq 7 2010-10-30 13:22 a root 6 2010-10-30 22:39 a john 5 2010-10-30 12:47 a john
Code:
$ atrm 7
Batch
The batch command can be used to execute on-time tasks when the load system average is under 0.8. It uses the atd service so it is installed with 'at' rpm package. By default any user can use 'batch' command and the files /etc/at.allow and /etc/at.deny can be used to restrict which users are allowed to use batch.
Batch examples
Code:
su - john john-$ batch at> echo 123 at> ctrl + d job 9 at 2010-10-30 13:10