Tuesday, January 24, 2017

Cron scheduler - cron.daily, cron.weekly, cron.monthly

Cron is a daemon that can be used to schedule the execution of recurring tasks according to a combination of the time, day of the month, month, day of the week, and week.

To check the RPM package for cron: 


[root@ ~]# rpm -qa | grep cron
cronie-anacron-1.4.11-14.el7_2.1.ppc64le
crontabs-1.11-6.20121102git.el7.noarch
cronie-1.4.11-14.el7_2.1.ppc64le

[root@~]#
-------------------------------------------------

To check the status of cron daemon:

[root@~]# /sbin/service crond status
Redirecting to /bin/systemctl status  crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2016-11-30 15:05:45 EST; 1 months 24 days ago
 Main PID: 4633 (crond)
   CGroup: /system.slice/crond.service
           └─4633 /usr/sbin/crond -n

Nov 30 15:05:45 hostname systemd[1]: Started Command Scheduler.
Nov 30 15:05:45 hostname systemd[1]: Starting Command Scheduler...
Nov 30 15:05:45 hostname crond[4633]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 71% if used.)
Nov 30 15:05:45 hostname  crond[4633]: (CRON) INFO (running with inotify support)
[root@ ~]#

------------------------------------------------------
The main configuration file for cron, /etc/crontab, contains the following lines:

[root@~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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


[root@~]#
-------------------------------------------------------------
minute   hour   day   month   dayofweek   command
Each line in the /etc/crontab file represents a task and has the format:


  • minute — any integer from 0 to 59
  • hour — any integer from 0 to 23
  • day — any integer from 1 to 31 (must be a valid day if a month is specified)
  • month — any integer from 1 to 12 (or the short name of the month such as jan or feb)
  • dayofweek — any integer from 0 to 7, where 0 or 7 represents Sunday (or the short name of the week such as sun or mon)
  • command — the command to execute (the command can either be a command such as ls /proc >> /tmp/proc or the command to execute a custom script)
As shown in the /etc/crontab file, it uses the run-parts script to execute the scripts in the /etc/cron.hourly/etc/cron.daily/etc/cron.weekly, and /etc/cron.monthly directories on an hourly, daily, weekly, or monthly basis respectively. The files in these directories should be shell scripts.

# 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
If a cron task needs to be executed on a schedule other than hourly, daily, weekly, or monthly, it can be added to the /etc/cron.d directory.
------------------------------------

How to test your cron jobs instantly :


[root@ jenkins]# crontab -l

* * * * * /etc/cron.weekly/test.sh &>>/tmp/cron_debug_log.log

[root@cjenkins]# 

 tail -f /tmp/cron_debug_log.log

will run your command for every minute and logged 

---------------------------------
[root@ ]# run-parts /etc/cron.weekly -v
/etc/cron.weekly/test.sh:
[root@ ]#
------------------------------------------

RHEL / CentOS  find out cron timings for /etc/cron.{daily,weekly,monthly}/
-----------------------------------------------------------------------------------------------
 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 also offers some special strings, which can be used in place of the five time-and-date fields:

Reference:

1) https://www.cyberciti.biz/faq/linux-when-does-cron-daily-weekly-monthly-run/
2) https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/3/html/System_Administration_Guide/ch-autotasks.html
3) https://help.ubuntu.com/community/CronHowto
4)https://www.digitalocean.com/community/tutorials/how-to-schedule-routine-tasks-with-cron-and-anacron-on-a-vps