A technical troubleshooting blog about Oracle with other Databases & Cloud Technologies.

Crontab

2 min read

Cron is named after Greek word “Chronos” that is used for time. It is a system process that will automatically perform tasks as per the specific schedule.

Cron is a unix utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon often termed as cron jobs.

It is a set of commands that are used for running regular scheduling tasks. Crontab stands for “cron table”. It allows to use job scheduler, which is known as cron to execute tasks.

Crontab (CRON TABLE) is a file which contains the schedule of cron entries to be run and at specified times, you can invoke it with the “crontab -e” command.

Benefits of Crontab.

* You more easily have control over when it runs. You control the minute, hour, day, etc, that it will execute

* It's not occupying memory in your system when it's not running.

* If it fails and exits for some reason, it will start up again when the proper time comes.

* It Delete old log files which is scheduled.

* It Sends out proactive notification email for scheduled jobs which is crossing threshold limit such as Database down, Load spike, Agent down, etc.
A crontab file has five fields for specifying day , date and time  followed by the command to be run at that interval. You can also specify a range of values.

*     *     *     *     *  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +-----> day of week (1 - 7) (monday = 1)
|     |     |     +-----------> month (1 - 12)
|     |     +-----------------> day of month (1 - 31)
|     +-----------------------> hour (0 - 23)
+-----------------------------> min (0 - 59)

How to Add/Modify Crontab

No need to restart your crontab as it will pick up your changes automatically when you use following command. User can view their crontab jobs with the help of following crontab command:

$ crontab -l

To remove your crontab tasks, use the following command:

$ crontab -r

To add or update job in crontab, use below given command:

crontab -e
# The following job runs a cleanup script a 05:00 each Sunday. Any output or errors from the script are piped to /dev/null to prevent a buildup of mails to root:

0 5 * * 0 /u01/app/oracle/scripts/cleanup.sh > /dev/null 2>&1
Command run your script on 3 minutes interval.	

*/3 * * * * /scripts/monitor.sh
# Execute at 3:45pm the first day of each month and do not send the results:

45 15 1 * * /u01/app/oracle/scripts/mensual.sh 1>/dev/null 2>&1
Command to execute on selected days. This example will run each Monday and Wednesday at 5 PM.	

0 17 * * mon,wed  /script/script.sh
# Execute from Monday to Friday at 6 PM

0 18 * * 1-5 /u01/app/oracle/scripts/full_backup.ksh >/dev/null 2>&1
# Execute every minute

* * * * * /u01/app/oracle/scripts/DB_UPTIME.sh