Showing posts with label CronJobs. Show all posts
Showing posts with label CronJobs. Show all posts

Sunday, 6 March 2016

CronJob Managment In CentOS

CRONTAB :

The crontab command submits, edits, lists, or removes cron jobs.
This command is a command and script schedular.
It runs task scheduling daily,hourly,weekly,monthly, etc.
It also allow users to setup their own schedules.
Example : We uses this command for system auditing ; running backup scripts.

Service : crond

It runs

]# crontab -l    -- Lists cron jobs for user himself.
]# crontab -lu proco  -- Lists cron jobs for user proco (Only root can execute this command).
]# crontab -eu proco  -- Edits user proco's cron file stored at "/var/spool/cron/proco".
]# crontab -ru proco -- Removes cron jobs for user himself.

Format :

 MIN    HOURS   DAYOFMONTH   MONTH   WEEKDAY        COMMAND
(0-59)  (0-23)  (1-31)       (1-12)  (0-6 0=SUN)    /vz/backup/vzbackup.sh >/dev/null 2>&1

Eg. :

41 21 * * * /vz/backup/vzbackup.sh >/dev/null 2>&1

 Here, we dump data using above "vzbackup.sh" script. Crontab will execute this script daily at 21'st hour of the day.
An * (asterisk), meaning all allowed values. To run a job every hour, specify an asterisk in the hour field.

To write the time to the console every hour on the hour, enter:

0 * * * * echo The hour is `date` . >/dev/console

IMP Files/Dir for crontab :

File:/etc/crontab   -- This file contains few directories as below. Script placed in a specific directory will execute according.
File:/var/spool/cron/{root,prem,etc}  -- This directory contains separate file for separate user. its a schedular file as below.
File:/etc/cron.deny  -- Specifies a list of users denied access to the crontab command.

[root@tlv1 etc]# 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
---------------------------------------------------

[root@tlv1 ~]# cat /var/spool/cron/root
41 21 * * * /vz/backup/vzbackup.sh >/dev/null 2>&1
---------------------------------------------------

Example:



Please set this cron to run every day at 11.55pm.
http://experiencesilk.com/duniya_dekhegi/cron_winners.php


Ans:
[root@experiencesilk cron]# cat root
55 23 * * * /usr/bin/php -q /var/www/html/duniya_dekhegi/cron_winners.php

=======================================



at command :

    The Linux “at” command used for scheduling jobs. But “at” command can set the job run only once. The “at” daemon can be used to run a command or script of your choice. From the command line, you can run the “at” time command to start a job to be run at a specified time. That time can be now; in a specified number of minutes, hours, or days; or at the time of your choice. Spool area for at command is /var/spool/at/ directory. For normal users, permission to use at command is determined by the file /etc/at.deny. The file /etc/at.deny is checked, every user name not mentioned in it is then allowed to use at.

SYNTAX :

]# at <time>

Here, time format can be :

now
midnight - (12:00AM)
noon - (12:00PM)
teatime - (4:00PM)
14:23 (24 hours format) OR 02:23PM (12 hours format)
now + 05/03/13 - Its MMDDYY format. Here date is 3rd May, 2013. (now = at same time)
now + 10 minutes
now + 2 hours
now + 1 day - After 24 hours.
now + 1 week - After 7 days.
3:00 05/03/13 - On specified date and time.


The Linux atq command lists the user’s pending jobs, unless the user is the superuser; in that case, everybody’s jobs are listed. The format of the output lines (one for each job) is: Job number, date, hour, job class.

The Linux atrm command deletes the scheduled jobs, identified by their job number.

[Use enter & ^D]
servcie : atd

Batch command is also a part of at command , but it executes job when system load is less. batch command is used to schedule a one-time task to be executed.
-----------------------------------------------------------------------------------------------

Crontab :

Crond is a daemon that executes scheduled commands.

SERVICE : crond

FILES   :
/var/spool/cron/  : here files are created for separate users including root.
/etc/crontab      :
/etc/cron.deny    : Restrict users from using crontab. not need to restart crond. because it everytime reads these files when user requests.

]# crontab -l [List cron job file for current user]
]# crontab -l -u procopania [Display cron job file for user procopania]


]# crontab -e [Edits cron job file for current user]
]# crontab -e -u procopania [Edits cron job file for user procopania]

#MIN (0-59)     HOUR (0-23)   DoM (1-31)     MONTH (1-12)     DoW (0-7)     CMD

   41             21             *                 *             *       /vz/backup/vzbackup.sh >/dev/null 2>&1

You can schedule multiple instances in the min, hour, dom etc. columns using a ',' as a separator.  For example, to run a job at 9:30PM on the 1st and 15th of each month, the syntax would be:

   30            21            1,15                 *              *     <JOB TO RUN>


Examples: In hours field I specified 4-7, Here it will include 4th,5th,6th and 7th hours.
        And 5,7 includes 5th and 7th hour. * includes all. For example, "0-23/2" can be used in the hours field to
          specify command execution every other hour ( I.e. "0,2,4,6,8,10,12,14,16,18,20,22").

To write the time to the console every hour on the hour, enter:

0 * * * * echo The hour is `date` . >/dev/console

Other than this 8 special strings may appear :
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 * * * *".

Note : It will execute @ 00:00 for most of the keywords.

]# crontab -r [Delete cron job file for current user]
]# crontab -r -u procopania [Delete cron job file for user procopania]

If you want to send email to specific ID then :

vi /etc/crontab
MAIL="ramesh" Or "mail@example.com" Or ""


OTHER DIRs :

/etc/anacrontab
/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

=============================