How to schedule a job on unix?

Jobs can be scheduled couple of ways on unix like cron, at.

Below is description how jobs can be scheduled using “at”. Note if there is an error generated during execution of the error or if output is not directed the output is emailed to the user who scheduled the job.

# to lists the jobs scheduled
$ at -l
3     2009-04-03  a  oracle

# how to schedule a job
$ at now + 5 min
at> ls
at> Press Ctrl-D
$ at -l
6     2009-04-03  a  oracle

# how to remove a scheduled job
$ at now + 5 min
at> ls
at> Press Ctrl-D
$ at -l
6     2009-04-03  a  oracle
# deletes job
$ at -r 6
# displays no jobs scheduled
$ at -l

Below are few examples:
# the following example schedules a job to run at 1:00 pm tomorrow
$ at 1:00 pm tomorrow
at> ls -lrt /tmp > /tmp/ls.log
at> Press CTRL-D

# the following example schedules a job to run in 5 minutes from now.
Note: Other options are min – minute, hour – hour, day, week, month
$ at now + 5 min
at> ls -lrt /tmp > /tmp/ls.log
at> Press CTRL-D

# to schedule commands in a file, the script file contains ls and ls -l, the output is emailed to the user
$ cat scriptfile
ls
ls -l
$ at now + 2 min < script filename

# list the details of the job #9
$ at -c 9
….
ls
ls -l

cron
Using cron one can schedule jobs, each user has their own crontab file. The cron file resides in /var/spool/cron/, only privileged user like root would have access to this directory.

On linux there is a log file /var/log/cron that records when a user runs a crontab to view/edit the cron and also when a command is scheduled through cron the log file gets updated. This file may be viewed by priviledged user.
Here is a sample logfile:
Apr 4 11:10:40 localhost crontab[28623]: (oracle) BEGIN EDIT (oracle)
Apr 4 11:10:53 localhost crontab[28623]: (oracle) REPLACE (oracle)
Apr 4 11:10:53 localhost crontab[28623]: (oracle) END EDIT (oracle)
Apr 4 11:13:05 localhost crontab[28723]: (oracle) LIST (oracle)
Apr 4 11:20:01 localhost crond[28940]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Apr 4 11:20:01 localhost crond[28943]: (oracle) CMD (/bin/ls)

Here are the options for crontab:
crontab -l # list the entries in the crontab
crontab -r # delete the cron file, once deleted there isn’t a way to restore unless there was backup taken of the crontab
crontab -e # open the crontab file for editing/viewing

To add entry in the crontab, below is the file format, to disable a job if one adds “#” at the beginning of the line.
Minute Hour DayOfMonth Month DayOfWeek command

Minute – 0-59
Hour – 0-23
DayOfMonth – 1-31
Month – 1-12 (1-Jan, 2-Feb,..,12-Dec)
DayOfWeek – 0-6 (0 – Sun, 1 – Mon, …, 6 – Sat)
If one of the values is “*” it means it will run every run interval, for example if the Minute is set to “*”, the every 1 minute interval i.e. 0-59.

Here are few examples:
1) 0-10 * * * * /bin/ls >> /tmp/ls.log
The above entry runs every minute of the hour for the first ten minutes /bin/ls, every day
2) 0 1 * * * /bin/ls >> /tmp/ls.log
The above entry runs every day at 1:00 am.
3) 0 1 1 * * /bin/ls >> /tmp/ls.log
The above entry runs at 1:00 am on first of the month
4) 0 1 * 3 * /bin/ls >> /tmp/ls.log
The above entry runs at 1:00 am in month of March
5) 0 1 * * 2 /bin/ls >> /tmp/ls.log
The above entry runs at 1:00 am on Tuesday of every week
5) # 0 1 * * 2 /bin/ls >> /tmp/ls.log
The above entry is commented out so it will not run, till “#” is removed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s