Corntabs in linux

 Linux Scheduling using Crontab
---------------------------------
 Daemon: crond
 Package: crontabs

[root@serverX ~]# rpm -q crontabs
[root@serverX ~]# rpm -qa | grep crontabs

[root@serverX ~]# yum install crontabs -y

Crontab Format:
==============
  *  *  *  *  *  <cmd>
1  2  3  4  5

1 = Minutes (0-59)
2 = Hour       (0-23)
        3 = day of month(1-31)
        4 = Months (1-12)
  5 = Day of Week (0-6) here 0 or 7 is sunday
        <cmd> = command to be execute

List of current Cronjob
-----------------------
[root@serverX ~]# crontab -l   ; list of current job for root

Crontab Remove:
--------------
[root@serverX ~]# crontab -r
[root@serverX ~]# crontab -l

Crontab Edit
--------------
[root@serverX ~]# crontab -e

Example 01:  Schedule a job every night 11.59 PM to shutdown the system:
------------------------------------------------------------------------
[root@serverX ~]# crontab –e
59  23   *   *   *   /usr/sbin/poweroff

[root@serverX ~]# crontab –l

Set the date 23.57 for job testing:
-----------------------------------
[root@serverX ~]# date
[root@serverX ~]# date +%T%p -s "23:57:00"
[root@serverX ~]# date

   or

[root@serverX ~]# date MMDDHHMMYY
[root@serverX ~]# date
[root@serverX ~]# systemctl status crond.service
[root@serverX ~]# systemctl restart crond.service
[root@serverX ~]# systemctl enable crond.service

[root@serverX ~]# watch date

Example 02: Take etc backup in .tar format under /backup directory on every Friday at midnight:
---------------------------------------------------------------------
[root@serverX ~]# date +\%Y-\%m-\%d

[root@serverX ~]# mkdir /backup
[root@serverX ~]# crontab –e

 00 00 * * 5  tar -cvf /backup/etc_$(date +\%Y-\%m-\%d).tar  /etc

[root@serverX ~]# date
[root@serverX ~]# cal              ; verify recent Thrusday
[root@serverX ~]# date MMDD2358YY
[root@serverX ~]# date

[root@serverX ~]# systemctl restart crond.service

[root@serverX ~]# watch date

[root@serverX ~]# cd /backup
[root@serverX ~]# ls

Example 03: Schedule job to run every five minute. Who are currently logged in server:
------------------------------------------------------------------------------
[root@serverX ~]# crontab –e
*/5  *  *  *  *  who >> /backup/login_$(date +\%Y-\%m-\%d-\%T)

Example 04: Check Ping Status at Night 1 to 5.00 AM every hour:
--------------------------------------------------------------
[root@serverX ~]# crontab –e

00 1-5 * * *  ping -c 4 172.25.11.1 >> /backup/ping

[root@serverX ~]# systemctl restart crond.service
[root@serverX ~]# cd /backup
[root@serverX ~]# ls

Example 05:  Schedule a job to run every six hours in a day:
------------------------------------------------------------
[root@serverX ~]# crontab –e

* 0,6,12,18  *  *  * cat /proc/meminfo | mail -s "Memory Status" admin@example.com

Example 06: Run a script at 01:00 am each weekday [Monday – Friday]:
------------------------------------------------------------------
00  01  *  *  1-5   /backup/db_backup.sh

Example 07: Run a cronjob December 31 at Mid Night (23:59)  every year:
-----------------------------------------------------------------------
 59 23 31 12 * /backup/backup.sh

Remove all cron jobs:
----------------------------
[root@serverX ~]# crontab –r
[root@serverX ~]# crontab –l

User based cron job:
--------------------
[root@serverX ~]# crontab –e -u student


===================== Thank you ===================

crontab -e
8 5 * * * mkdir /root/desktop/linux

8 5 * * * rsync -r /foot/Desktop/all/ /root/Desktop/linux
07 11 * * * rm -rf /crondemo/*     [crondemo dir name]
ls /var/spool/cron
30 14 30 4 7 reboot
0 3 1,3,5 * /mon.sh [ date of 1 3 5 ]
15 3 1-10 5 * /mon2sh [ date 1 to 10]
30 16 1-10,15-25 * /mon3.sh date 1to10 and 15-25]

run the backup script /bkp.sh every sat midnight 12 am
-------------------------------------------------------
* 0 * * 7 /bkp.sh  [sat midnight means sunday]
*/5 */1 * * * /bkp.sh [every 5 min every houre]

47 19 29 4 * echo "CORN IS SUCCESSFUL" >/dev/pts/0
watch ls
 chmod +x /bkp.sh [ for give execute permission bkp.sh]
corntab -lu student
corntab -eu sudent [for student schedule]
vim /etc/corn.deny



No comments:

Post a Comment