Saturday, August 13, 2016

CRON – SCHEDULER

https://crontab.guru   --- check con job status 


[root@Tusharjahdav ~]# rpm -qa | grep cron  ------to check install package
cronie-1.4.4-12.el6.x86_64
cronie-anacron-1.4.4-12.el6.x86_64
crontabs-1.10-33.el6.noarch
[root@Tusharjahdav ~]# rpm -qa | grep -i cron
cronie-1.4.4-12.el6.x86_64
cronie-anacron-1.4.4-12.el6.x86_64
crontabs-1.10-33.el6.noarch

[root@Tusharjahdav ~]# /etc/init.d/crond start   ----start cron service
[root@Tusharjahdav ~]#  service crond start   ---- start cron service
-----------------------------------------------------------------------------------------------------------------
[root@Tusharjahdav ~]# vi bckup.sh   ------create backup.sh file

!/bin/bash
rsysc -aprv /usr/local/*  /opt/cron   ------script
-----------------------------------------------------------------------------------------------------------------
[root@Tusharjahdav ~]# vi H.sh

ync;    -----------------------------------------------------------------script
echo 0 > /proc/sys/vm/drop_caches;
sync;
echo 1 > /proc/sys/vm/drop_caches;
sync;
echo 2 > /proc/sys/vm/drop_caches;
sync;
echo 3 > /proc/sys/vm/drop_caches;
sync;
swapoff -av;
swapon -av;
free -m
-----------------------------------------------------------------------------------------------------------------
[root@Tusharjahdav ~]# chmod +X bckup.sh  -------provide permission
[root@Tusharjahdav ~]# chmod +X H.sh         -------provide permission
-----------------------------------------------------------------------------------------------------------------
To edit your crontab file, type the following command:
[root@Tusharjahdav ~]# $ crontab -e ----------------------------current user

[root@Tusharjahdav ~]# crontab –u harry –e  ---------------------------specific user

15 17 * * * /backup.sh

15 17 * * * /H.sh

[root@Tusharjahdav ~]# cd /var/spool/cron/   ------all cron job file stored in this path

[root@Tusharjahdav cron]# ls
Pranay  --------------------------------cron job file created as a user name

[root@Tusharjahdav cron]# cat pranay  ----to check cron job
15 17 * * * /backup.sh
15 17 * * * /H.sh
-----------------------------------------------------------------------------------------------------------------
How cron works

[root@Tusharjahdav cron]# /etc/init.d/crond start   -----start cron

[root@Tusharjahdav cron]# tail -f /var/log/cron   -------check cron log files

Jan  5 03:30:01 Tusharjahdav CROND[6335]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan  5 03:30:01 Tusharjahdav anacron[6023]: Job `cron.weekly' started
Jan  5 03:30:01 Tusharjahdav anacron[6023]: Job `cron.weekly' terminated
Jan  5 03:30:01 Tusharjahdav anacron[6023]: Normal exit (2 jobs run)
Jan  5 03:38:57 Tusharjahdav crontab[6345]: (root) BEGIN EDIT (pranay)
Jan  5 03:40:01 Tusharjahdav CROND[6350]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan  5 03:42:15 Tusharjahdav crontab[6345]: (root) REPLACE (pranay)
Jan  5 03:42:15 Tusharjahdav crontab[6345]: (root) END EDIT (pranay)
Jan  5 03:43:25 Tusharjahdav crontab[6353]: (root) BEGIN EDIT (pranay)
Jan  5 03:44:21 Tusharjahdav crontab[6353]: (root) END EDIT (pranay)
-----------------------------------------------------------------------------------------------------------------
[root@Tusharjahdav cron]# which crond  -----cron main file location
/usr/sbin/crond

#cron -----run every 1M set as default

Can we create in second cron job ---- No because cron by default set as a 1 Minutes

Note: If you want to set in second so we can used for a loop
-----------------------------------------------------------------------------------------------------------------
Syntax of crontab
Your cron job looks like as follows:

1 2 3 4 5 /path/to/command arg1 arg2

Where,
1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31) ---Date
4: Month (0-12 [12 == December])
5: Day of the week (0-7 [7 or 0 == sunday])
/path/to/command - Script or command name to schedule
Same above five fields structure can be easily remembered with following
diagram:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)(Date )
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
18th of Friday -----night ----2:21    ---run job

21 02 18 * 05    /backup.sh   |  00 20 * * 3  ---run every Wednesday
-----------------------------------------------------------------------------------------------------------------

Every new year we can run job

00 00 01 01 *  /backup.sh
-----------------------------------------------------------------------------------------------------------------
We can set also

@year    /backup.sh
@mingnigh   /backup.sh
@reboot     /backup.sh
-----------------------------------------------------------------------------------------------------------------
[root@KH_salessvr_4 cron]# tail -f root
0 * * * * /usr/sbin/ntpdate -u time.nist.gov > /dev/null 2>&1 ; /sbin/hwclock --               systohc

0 23 * * * /opt/DatabaseBackUP/databaseBackup.sh

[root@KH_salessvr_4 /]# crontab -l
0 * * * * /usr/sbin/ntpdate -u time.nist.gov > /dev/null 2>&1 ; /sbin/hwclock --systohc

0 23 * * * /opt/DatabaseBackUP/databaseBackup.sh

[root@proxy2 cron]# cat root

0 2 * * * /usr/local/bin/squid-analyzer > /dev/null 2>&1
0 */2 * * * /bin/H
*/10 * * * * /bin/S

[root@proxy2 cron]# crontab -l

0 2 * * * /usr/local/bin/squid-analyzer > /dev/null 2>&1
0 */2 * * * /bin/H
*/10 * * * * /bin/S

[root@proxy2 cron]# cat /bin/S
/etc/init.d/squid reload

[root@proxy2 cron]# cat /bin/H
free -m;
sync;
echo 0 > /proc/sys/vm/drop_caches;
sync;
echo 1 > /proc/sys/vm/drop_caches;
sync;
echo 2 > /proc/sys/vm/drop_caches;
sync;
echo 3 > /proc/sys/vm/drop_caches;
sync;
swapoff -av;
swapon -av;
free –m

[root@Tusharjahdav ~]# cp H /opt/   ----copy H.sh file from root/ location to /opt
[root@Tusharjahdav ~]# cd /opt/
[root@Tusharjahdav opt]# ls
corn  H  rh  software  users


[root@Tusharjahdav opt]# cp H /bin/----copy H.sh file from/opt/  to /bin/

[root@Tusharjahdav opt]# chmod +x /opt/H   -----given to permission        
[root@Tusharjahdav opt]# chmod +x /bin/H

[root@Tusharjahdav opt]# H   ------------------run H command for  memory free
/bin/H: line 1: ync: command not found
swapoff on /dev/sda3
swapon on /dev/sda3
swapon: /dev/sda3: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/sda3: pagesize=4096, swapsize=2113929216, devsize=2113929216
             total       used       free     shared    buffers     cached
Mem:           992        188        803          0          0         33
-/+ buffers/cache:        155        837
Swap:         2015          0       2015

[root@Tusharjahdav opt]#cp Cc /bin/
[root@Tusharjahdav opt]#chmod +x Cc
[root@Tusharjahdav opt]#chmod +x /bin/Cc
[root@Tusharjahdav opt]# Cc   --------------shutdown command
----------------------------------------------------------------------------------------------------------------
cd /etc/net/
-rw-r--r-- 1 root root 28971 Mar  5 17:49 netma
-rw-r--r-- 1 root root   325 Mar  4 10:05 none

vi /bin/nonet
cp /etc/net/none /etc/hosts;
squid -k reconfigure;
echo We have successfully added Public IP!!!!!!!!
vi /bin/netmac
cp /etc/net/netma /etc/hosts;
squid -k reconfigure;
echo "We have successfully added Netmagic Private IP"
Special string Meaning
@reboot -----------Run once, at startup.
@yearly -----------Run once a year, "0 0 1 1 *".
@annually ---------(same as @yearly)
@monthly ---------Run once a month, "0 0 1 * *".
@weekly -----------Run once a week, "0 0 * * 0".
@daily -------------Run once a day, "0 0 * * *".
@midnight---------(same as @daily)
@hourly -----------Run once an hour, "0 * * * *".

[root@Tusharjahdav cron]# vi /etc/crontab

Typical /etc/crontab file entries:
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

Directory Description
/etc/cron.d/ Put all scripts here and call them from /etc/crontab
file.
/etc/cron.daily/       Run all scripts once a day
/etc/cron.hourly/    Run all scripts once an hour
/etc/cron.monthly/ Run all scripts once a month
/etc/cron.weekly/   Run all scripts once a week

[root@Tusharjahdav ~]# ps ---------------to check terminal tty number
  PID TTY          TIME CMD                      
 3272 pts/1    00:00:00 bash
 3287 pts/1    00:00:00 ps

[root@Tusharjahdav ~]# ps ---------------to check terminal tty number
  PID TTY          TIME CMD
 3272 pts/1    00:00:00 bash
 3287 pts/1    00:00:00 ps

# - by default cron run every 1 minutes  

*/1 * * * * echo “Good Morning” > /dev/pts/4   --------- set every 1m cron job

[root@Tusharjahdav cron.weekly]# vi /etc/crontab ---To check all cron jobs
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

30 18 * * * harry   run-parts  /opt  -----------------to run all script cron job from  /opt folder
30 18 * * * harry  /opt/bckup.sh   -------------------------to run only one mansion script
/etc/cron.allow and /etc/cron.deny
If a user is only in /etc/cron.allow, then all others are denied
If a user is only in /etc/cron.deny then all others are allowed/not
affected
If cron.deny is touched, then no users is allowed to create a crontab
If cron.allow is touched, then no users is allowed to create a crontab

vi /etc/cron.deny  -----add user name in this file
sachin
rajesh
sohel

touch /etc/cron.allow  --- to block all users

If /etc/cron.allow  file exist then all others users are block and if you want allow same of user just put user name in this file.

vi / etc/cron.allow  -----allow user
champu
harry

My system will shut down in night & cron job not run on set time in this case anacron will run the job

vi /etc/anacronab  ----------------we will manage

Anacron - Job left by cron will be execute by anacron 
anacron is another tool designed for systems which are not always on, such as home computers.
While cron will not run if the computer is off, anacron will simply run the command when the computer is next on (it catches up with things).

at Scheduler  : at used for one time

[root@Tusharjahdav /]# at -f /opt/H now + 1 hour  -----run after 1 hour
job 2 at 2016-01-06 06:30

[root@Tusharjahdav /]#at –f /opt/H 11:00 next month

[root@Tusharjahdav /]# /etc/init.d/atd status  ---check at status
atd (pid  1976) is running...

'at' executes a command once on a particular day, at a particular time. at
will add a particular command to be executed.
Examples:
$ at 21:30
Command syntax:
$ atrm job_no
Will delete the job "job_no" (use atq to find out the number of the job)
$ at -f myjobs.txt now + 1 hour
$ at -f myjob now + 1 min
$ at 10 am tomorrow
$ at 11:00 next month
$ at 22:00 today
$ at now + 1 week
$ at noon

mins hrs DOM MOY DOW
00-59 00-23 1-31 1-12 0-7 (0=Sun 1=Mon, 2=Tue, 3=Wed,4=Thu, 5=Fri,
6=Sat and 7=Sun)

[root@Tusharjahdav opt]# vi bckup.sh------create backup.sh file

#!/bin/bash
rsysc -aprv /opt/*  /home/data

wq!

[root@Tusharjahdav opt]# chmod +x bckup.sh

[root@Tusharjahdav opt]# tail –f /var/log/cron


 
[root@Tusharjahdav /]# crontab -u harry -e


*/5 * * * * /opt/backup.sh   -----Run every 5 m

cp /var/spool/cron/* /home/data/  ----------------take backup of cron
[root@Tusharjahdav /]# tail -f /var/log/cron
Jan  6 23:46:21 Tusharjahdav crontab[4135]: (root) END EDIT (harry)
Jan  6 23:46:24 Tusharjahdav crontab[4138]: (root) BEGIN EDIT (harry)
Jan  6 23:48:12 Tusharjahdav crontab[4138]: (root) END EDIT (harry)
Jan  6 23:49:22 Tusharjahdav crontab[4150]: (root) BEGIN EDIT (harry)
Jan  6 23:49:38 Tusharjahdav crontab[4150]: (root) REPLACE (harry)
Jan  6 23:49:38 Tusharjahdav crontab[4150]: (root) END EDIT (harry)
Jan  6 23:50:01 Tusharjahdav crond[1965]: (harry) RELOAD (/var/spool/cron/harry)
Jan  6 23:50:01 Tusharjahdav CROND[4162]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan  6 23:50:01 Tusharjahdav CROND[4161]: (harry) CMD (/backup.sh)
Jan  6 23:50:01 Tusharjahdav CROND[4160]: (root) CMD (/bckup.sh)

[root@Tusharjahdav data]# cd /var/spool/cron/  -----all cron job location
[root@Tusharjahdav cron]# ls
harry  pranay  root

[root@Tusharjahdav ~]# vi /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


At- Scheduler

Task :  used for one time
[root@Tusharjahdav ~]# at -f /opt/H now + 1 hour  ----run after 1 hour
job 3 at 2016-01-11 01:03

[root@Tusharjahdav ~]# at -f /opt/H 11:00 next month   ---run next month
job 4 at 2016-02-11 11:00

[root@Tusharjahdav ~]# /etc/init.d/atd status    ---check at status
atd (pid  1973) is running...

Run cron job alternate week day

In this post we will learn,Run cron job alternate week day.
Today, I met with very good question i.e how to run cron job alternate week day. The user wants to run the script in alternate Saturday.When I read this question first time,it make me to take a pause for a while. Later thinking on some more logic,I ended up with nice way to accomplish this task .

What is cron

Cron is a software utility,basically a job scheduler in Linux and Unix like operating system.
Cron information are saved in crontab which is also know as cron table.The format of writing

crontab is given below
Minute Hour date-Of-Month Month Day-Of-Week /path/of/command-script

Task : We got a task to run cron job alternate week day. For eg. run a cronjob in alternate Monday,Tuesday,thursday etc.

See given below examples

Time to run script : 12.30 AM
Month : Run the script every month
Day of Week : Alternate day of week

 

Day of week represented in Crontab as follows

Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Firday = 5
Saturday = 6

On alternate Monday
30 00 1-2,15-16,29-31 * 1 give-path-of-script-command
On alternate Tuesday
30 00 1-3,15-17,29-31 * 2 give-path-of-script-command
On alternate Wednesday
30 00 1-4,15-18,29-31 * 3 give-path-of-script-command
On alternate Thursday
30 00 1-5,15-19,29-31 * 4 give-path-of-script-command
On alternate Friday
30 00 1-6,15-20,29-31 * 5 give-path-of-script-command
On alternate Saturday
30 00 1-7,15-20,29-31 * 6 give-path-of-script-command
On alternate Sunday
30 00 1-7,15-20,29-31 * 0 give-path-of-script-command


crontab entry for: 2nd and 4th  Saturday of each month
The second Saturday of the month falls on one (and only one) of the dates from the 8th to the 14thinclusive. Likewise, the fourth Saturday falls on one date between the 22nd and the 28th inclusive.

You may think that you could use the day of week field to limit it to Saturdays (the 6 in the line below):
00  01  8-14,22-28  *  6  /path/to/myscript

crontab entry for: 2nd Saturday of each month


No comments:

Post a Comment