Archives

Job Scheduling Cron, Anacron and at, batch

                                                                                                                  Automated System Tasks

Linux Job Scheduling Command

 

RHEL 6 comes with the following automated task utilities command:

1.      cron,

2.      anacron

3.      at

4.      batch

 

“cron” and “anacron” difference

Both, cron and anacron, are daemons that can schedule execution of recurring tasks to certain point in time defined by the exact time, day of the month, month, day of the week, and week

 

Cron

anacron

“cron” will not execute the schedule job if system goes down.

“anacron” will resume the scheduled job (if system is down) and execute it when system is up.

Required Package

[root@server1 Desktop]# rpm -qa cronie

cronie-1.4.4-7.el6.x86_64

Required Package

[root@server1 Desktop]# rpm -qa cronie-anacron

cronie-anacron-1.4.4-7.el6.x86_64

Configuring “cron” job file

/etc/crontab

Configuring “anacron” jon file

/etc/anacrontab

Minimum granularity is minute (i.e Jobs can be scheduled to be executed every minute)

 

 

Minimum granularity is only in days

Cron job can be scheduled by any normal user ( if not restricted by super user

Anacron can be used only by super user ( but there are workarounds to make it usable by normal user )

Ideal for servers

Ideal for desktops and laptops

Use cron when a job has to be executed at a particular hour and minute

Use anacron when a job has to be executed irrespective of hour and minute

 

Starting and Stopping the “cron” service

The “cron” and “anacron” jobs are both picked by  the “crond” service.

 

service crond start | stop | status | reload | restart | force-restart

 

chkconfig crond on | off | –list

 

crontab command

1.      crontab -l         –           job list

2.      crontab -u        –           user based

3.      crontab -e        –           edit or new cron job

4.      crontab -r         –           remove cron job

 

Controlling Access to “cron”

To restrict access the cron file by user

 

/etc/cron.allow

/etc/cron.deny

 

·      If the cron.allow file exists, only users listed in the file are allowed to use cron, and the cron.deny file is ignored.

 

·      If the cron.allow file does not exist, users listed in the cron.deny file are not allowed to use Cron.

 

·      The root user can always use cron, regardless of the usernames listed in the access control files.

 

 

 

 

 

 

You can control the access also through Pluggable Authentication Modules (PAM). The settings are stored in the /etc/security/access.conf file. For example, after adding the following line to the file, no other user but the root user can create crontabs:

 

-:ALL EXCEPT root :cron

 

The forbidden jobs are logged in an appropriate log file or, when using “crontab -e”, returned to the standard output. For more information, refer to access.conf.5 (that is, man 5 access.conf).

 

 

Configuring “cron” job file

 

[root@server1 Desktop]# cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

 

# For details see man 4 crontabs

 

# Example of job definition:

# .—————- 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

# |  |  |  |  |

# *  *  *  *  * user-name command to be executed

 

 

Configuring “anacron” job file

 

[root@server1 Desktop]# cat /etc/anacrontab

# /etc/anacrontab: configuration file for anacron

 

# See anacron(8) and anacrontab(5) for details.

 

SHELL=/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

# the maximal random delay added to the base delay of the jobs

RANDOM_DELAY=45

# the jobs will be started during the following hours only

START_HOURS_RANGE=3-22

 

#period in days   delay in minutes   job-identifier   command

1          5          cron.daily                    nice run-parts /etc/cron.daily

7          25        cron.weekly                 nice run-parts /etc/cron.weekly

@monthly 45   cron.monthly               nice run-parts /etc/cron.monthly

 

Cron job example backup script

# record the memory usage of the system every monday

# at 3:30AM in the file /tmp/meminfo

30 3 * * mon cat /proc/meminfo >> /tmp/meminfo

# run custom script the first day of every month at 4:10AM

10 4 1 * * /root/scripts/backup.sh

 

 

“cron” jab examples

 

1.      The user mugu must configure a cron jon that runs daily at 15:25 locate time and execute /bin/echo hiya

 

[root@server1 Desktop]# crontab -u mugu -e

no crontab for mugu – using an empty one

crontab: installing new crontab

 

25        15        *          *          *          /bin/echo hiya

 

:wq!

 

[root@server1 Desktop]# crontab -u mugu -l

25        15        *          *          *          /bin/echo hiya

 

Remove the cron job schedule particular user

[root@server1 Desktop]# crontab -u mugu -l

25        15        *          *          *          /bin/echo hiya

 

[root@server1 Desktop]# crontab -u mugu –r

 

[root@server1 Desktop]# crontab -u mugu -l

no crontab for mugu

 

2.      How to set the every 1 min /bin/echo hiya

 

[root@server1 Desktop]# crontab -e

no crontab for root – using an empty one

crontab: installing new crontab

 

[root@server1 Desktop]# crontab -l

*/1       *          *          *          *          /bin/echo hiya

 

[root@server1 Desktop]# mail

Heirloom Mail version 12.4 7/29/08.  Type ? for help.

“/var/spool/mail/root”: 1 message 1 new

>N  1 Cron Daemon           Thu Oct 25 18:35  21/706   “Cron /”

& 1

Message  1:

From root@server1.example.com  Thu Oct 25 18:35:02 2012

Return-Path:

X-Original-To: root

Delivered-To: root@server1.example.com

From: root@server1.example.com (Cron Daemon)

To: root@server1.example.com

Subject: Cron /bin/echo hiya

Content-Type: text/plain; charset=UTF-8

Auto-Submitted: auto-generated

X-Cron-Env:

X-Cron-Env:

X-Cron-Env:

X-Cron-Env:

X-Cron-Env:

Date: Thu, 25 Oct 2012 18:35:01 +0530 (IST)

Status: R

 

hiya

 

& q

Held 1 message in /var/spool/mail/root

You have mail in /var/spool/mail/root

 

3.      Restrict user babu not access cron or modifiy the job file.

 

[root@server1 Desktop]# vim /etc/cron.deny

babu

 

:wq!

 

[root@server1 Desktop]# su – babu

 

[babu@server1 ~]$ crontab -e

You (babu) are not allowed to use this program (crontab)

See crontab(1) for more information

 

[babu@server1 ~]$ logout

 

4.      Deny to all users except root to run cron schedule.

 

[root@server1 Desktop]# vim /etc/security/access.conf

Shift+g

– : ALL EXCEPT root : cron

:wq!

 

[root@server1 Desktop]# su – mugu

[mugu@server1 ~]$ crontab -e

 

Permission denied

You (mugu) are not allowed to access to (crontab) because of pam configuration.

[mugu@server1 ~]$ logout

 

[root@server1 Desktop]# su – babu

[babu@server1 ~]$ crontab -e

 

Permission denied

You (babu) are not allowed to access to (crontab) because of pam configuration.

[babu@server1 ~]$ logout

 

[root@server1 Desktop]# crontab -e

no crontab for root – using an empty one

crontab: no changes made to crontab

 

5.      Install the Cron Schedule for jeff user to display “Hello” on daily 5:30

 

Crontab –u jeff –e

30        05        *          *          *          /bin/echo “hello”

 

:wq!

 

6.      Add a job on Cron schedule to display Hello World on every two Seconds in terminal 8

Crontab –e

*/2       *          *          *          *          /bin/echo “Hello World” > /dev/tty8

 

 

 

7.      Add a cron schedule to take full backup of /home on every day at 5:30 pm to /dev/st0 device

[root@server1 Desktop]# rpm -qa dump

dump-0.4-0.6.b42.el6.x86_64

 

Crontab –e

30        05        *          *          *          /sbin/dump -0u /dev/st0 /dev/sda7

 

Or

 

30        05        *          *          *          /sbin/dump -0u –f /dev/st0 /home

 

 

Anacron Example

 

1.      Anacron joy daily start 6am to 7am each job delay time 1min

 

RANDOM_DELAY — maximum number of minutes that will be added to the delay in minutes variable which is specified for each job

 

The minimum delay value is set, by default, to 6 minutes.

 

If RANDOM_DELAY is, for example, set to 12, then between 6 and 12 minutes are added to the delay in minutes for each job in that particular anacrontab.

 

RANDOM_DELAY can also be set to a value below 6, including 0. When set to 0, no random delay is added. This proves to be useful when, for example, more computers that share one network connection need to download the same data every day.

                                 

START_HOURS_RANGE — interval, when scheduled jobs can be run, in hours

In case the time interval is missed, for example due to a power failure, the scheduled jobs are not executed that day.

 

[root@server1 Desktop]# touch script.sh

[root@server1 Desktop]# touch test.txt

 

[root@server1 Desktop]# chmod 755 script.sh

 

[root@server1 Desktop]# echo “date >> /root/Desktop/test.txt” > script.sh

 

[root@server1 Desktop]# cat script.sh

date >> /root/Desktop/test.txt

 

[root@server1 Desktop]# ./script.sh

 

[root@server1 Desktop]# cat test.txt

Fri Oct 26 07:03:56 IST 2012

 

[root@server1 Desktop]# anacron -u

[root@server1 Desktop]# vim /etc/anacrontab

 

SHELL=/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

RANDOM_DELAY=1

START_HOURS_RANGE=7-8

 

1          0          cron.daily                    /bin/sh /root/Desktop/script.sh           

1          5          cron.daily                    nice run-parts /etc/cron.daily

7          25        cron.weekly                 nice run-parts /etc/cron.weekly

@monthly 45   cron.monthly               nice run-parts /etc/cron.monthly

 

:wq!

 

[root@server1 Desktop]# updatedb

[root@server1 Desktop]# anacron –u  – update date

[root@server1 Desktop]# anacron –n  – job start now

[root@server1 Desktop]# anacron –f  – force job start

[root@server1 Desktop]# anacron -f

[root@server1 Desktop]# cat test.txt

Fri Oct 26 07:03:56 IST 2012

Fri Oct 26 07:12:24 IST 2012

 

 

“at” and “batch” command

 

The at” command is used to schedule a one-time task at a specific time.

The batch” command is used to schedule a one-time task to be executed when the systems load average drops below 0.8.

 

Both “at” and “batch” required package and service

[root@server1 Desktop]# rpm -qa at

at-3.1.10-43.el6.x86_64

 

service atd status | start | restart | force-restart | stop

atd (pid  2505) is running…

 

[root@server1 Desktop]# chkconfig atd –list | on | off

atd                   0:off    1:off    2:off    3:on     4:on     5:on     6:off

 

Controlling access “at”

/etc/at.deny

 

“at” utility command

 

atd  –    run jobs queued for later execution 

atq  –    list queue

atrm –   delete the job 

atrun-   run jobs queued for later execution

 

“at” command example

 

1.      Script start 7:46 am

 

[root@server1 Desktop]# at 7:46

at> /bin/sh /root/Desktop/script.sh

at>                                                                    press->ctrl+d

job 2 at 2012-10-26 07:46

 

[root@server1 Desktop]# atq

2          2012-10-26 07:46 a root

 

[root@server1 Desktop]# cat test.txt

Fri Oct 26 07:46:00 IST 2012

 

 

 

2.      How to Remove the job

[root@server1 Desktop]# at 07:59

at> cat /etc/fstab >> test.txt

at>

job 3 at 2012-10-26 07:59

[root@server1 Desktop]# atq

3          2012-10-26 07:59 a root

[root@server1 Desktop]# atrm 3

[root@server1 Desktop]# atq

 

3.      The job run immediately

[root@server1 Desktop]# at now

at> uptime >> /root/Desktop/test.txt

at>

job 4 at 2012-10-26 07:53

 

[root@server1 Desktop]# cat test.txt

07:54:09 up  1:24,  2 users,  load average: 0.00, 0.00, 0.00

 

4.      The job run 1 minutes lately current time

 

[root@server1 Desktop]# at now + 1 minutes

at> uptime > /root/Desktop/test.txt

at>

job 6 at 2012-10-26 07:59

[root@server1 Desktop]# atq

6          2012-10-26 07:59 a root

[root@server1 Desktop]# cat test.txt

 07:57:00 up  1:27,  2 users,  load average: 0.00, 0.00, 0.00

 

5.      “batch” command example

 

[root@server1 Desktop]# batch

at> uptime >> /root/Desktop/test.txt

at>

job 7 at 2012-10-26 08:00