Bagi Chandrakasan
Scheduling jobs with crontab

Scheduling Jobs With Crontab

CRONTAB to schedule jobs in linux

CRONTAB (cron table) is used to schedule jobs to run at specific time or at regular intervals. "at" can be used as well for a quick schedule. A super user can control crontab entries for other users. cron.allow and cron.deny files allow deny individual user permissions to run crontab.

All examples and code below is for the current logged in user.

# crontab usage:
crontab -l     # list crontab
crontab -e     # edit crontab entries
crontab -d     # delete crontab entry
crontab  file  # replaces current crontab with entries in file.
               # Prefer this as a backup if crontab gets accidentally deleted.


# CRON entries
# super user
* * * * * user  command_to_run

#logged in user
* * * * * command_to_run

An asterisk "*" indicates all possible values (every minute, every hour etc). Comma operator "," for a list of values and "-" for ranges is used. Slash "/" can be used on numeric fields to skip. Example */2 in minute field will run the command every two minutes. Some older versions don’t support this operator.

First five positions are:

  1. Minute [0-59]
  2. Hour [0-23]
  3. Day of the Month [1-31]
  4. Month [1-12] or three character value [Jan, Feb, Mar,…]
  5. Day of Week [0-7] (0 or 7 is Sunday) or [Sun, Mon,…]

Command is till end of line including redirects.

There are additional ways to specify frequency (@monthly, @daily etc) - please refer to documentation online.

Note: When running oracle jobs, environment variables need to be set (SID, PATH etc). Frequently, they can be set inside the command script instead of adding them to cron entry.


### Run archiver script every 15 minutes
0,15,30,45 * * * * /home/user/

### Run job every minute
* * * * * /home/user/

### Run job daily at 5:30AM and 5:30PM
30 5,17 * * * /home/user/ > /tmp/runlog 2>&1

### Run a job on Second Wednesday every month at 9am.
### 8-14 for second week. date +%u gives Weekday [1-Mon, 7-Sun]
### Though this script runs every day from 8th till 14th, the if condition limits to Wednesday only.
0 9 8-14 * * if [  $(date +\%u) = 3  ]; then /home/user/ > /tmp/logfile 2>/tmp/errlog; fi

### Run "Daily" notification script - run only Mon-Fri at 10:30AM
30 10 * * 1-5 /home/user/ > /tmp/_file_type_log_file

###  Run job every 15 minutes on 4th till 6th of the month.
*/15 * 4-6 * * /home/user/ >> /tmp/logfile.out 2>&1

### Find *.dat files in /opt/app1 directory older than 30 days and remove them.
### Job run at 5:30AM on Thursdays
30 5 * * 4 find /opt/app1 -type f -mtime +30 -name \*.dat -exec rm -f {} \; >> /tmp/logfile-clean.out 2>&1

### Check for /opt/app1/app.lock file at midnight and 12pm. If does not exist, send an email message to user
0 0,12 * * * test -f /opt/app1/app.lock|| echo "ERROR: `uname -n`, `date`, There is no app.lock file created" |mail -s "ERROR `uname -n`: App file lock error"


The last example in the crontab runs at specific times looking for a file created by the app. What if the file creation happens at anytime and you need to act right away? One option is to check for the file every minute using regular crontab entries.

There is an easy way using incrontab - acts similar to crontab but is based on inotify and triggered on events related to files/directories.

Please refer to the manual online for more details.

Part of my data load scripts analyze partitions and full tables after each load. Full table analyze statements estimating statistics can take longer and added time to the overall completion time for the job. Using incrontab i decoupled the full table analyze commands from the main script and made it run separately.

### Monitor for file "appload" in /opt/app1/monitor directory. On Creation, run the script /opt/app1/scripts/ Pass the full path and file name.

/opt/app1/monitor/appload IN_CREATE,IN_NO_LOOP /opt/app1/scripts/ $@/$#

### will receive the full file name. This is a regular shell script which can be used to trigger additional process like creating external tables, calling a sql loader or anything.