Linux Services Organization

Our goal, introduce Linux services to the enterprise world.
Contact us in contact@linuxsv.org

Linux Services Organization : Automating tasks Linux Server

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 :


$ 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


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 :


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


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

$ su - john
john-$ at now + 10 minutes
at> echo 123
at> Ctrl + d
job 5 at 2010-10-30 12:47

In 10 minutes 'echo 123' will be executed as user 'john'


john-$ at now + 10 hours
at> /home/john/test.sh
at> Ctrl + d
job 6 at 2010-10-30 22:39

In 10 hours /home/john/test.sh will be executed as user 'john'


john-$ su - root
$ at 01:00 12/30/2010
at> /sbin/reboot
at> Ctrl + d
job 7 at 2010-12-30 01:00

At 2010-12-30 01:00 the command /sbin/reboot will be executed as root


$ 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
Lists all atq scheduled tasks

$ atrm 7
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

su - john
john-$ batch
at> echo 123
at> ctrl + d
job 9 at 2010-10-30 13:10

Run 'echo 123' as user john when the system load average is under 0.8. Commands atq and atrm allows manipulate batch scheduling.

Questions

1.- By default only root user can schedule 'at' jobs (true/false)

2.- Cron tasks can be removed with 'atrm' command (true/false)

3.- Batch tasks can be removed with the command 'atqrm' (true/false)

4.- If atd service is not running at jobs can not be scheduled? (true/false)

5.- Which command will execute user john to schedule the execution of 'echo 123' in 10 minutes ?

6.- What must be done in order to deny cron to all users except root ?

7.- Which cron line must be added in root cron in order to execute 'echo cron-test' on terminal 2 every Saturday from 0-8h every 5 minutes ?

8.- Batch command uses 'batchd' services in order to schedule batch tasks. (true/false)

9.- Which cron line executes 'echo test' on 05:01 every Monday ?
A - 01 05 * * 0 echo test
B - 05 01 1 * * echo test
C - 01 05 * * 1 echo test
D - 05 01 * * 1 echo test

10.- Which cron line executes 'echo test' the first of month at 00:00,01:00,02:00 ?
A - 0-2 00 1 * * echo test
B - 00 0-2 1 * * echo test
C - 0-2 00 * 1 * echo test
D - 00 0-2 * 1 * echo test

Labs

1.- schedule a cron job for user 'john' to delete all files that begin as 'test-' in his home every day at 02:30h.

2.- schedule a one-time job execution to put the system into 'init 3' on 30/11/2010 at 05:00.

3.- schedule a one-time job execution of 'echo system is under 0.8 > /tmp/load-under-0.8' as user cate when the system load average is under 0.8.

-- This page is part of Linux Server online tutorial --