Not a member? Why not join us or log in? (Free to Post your Articles and Blogs on Technology.)

How to Schedule a Unix cron job

Home > Linux>
Dated : July 22, 2009
Follow us on Twitter
Connect with us on Facebook
Subscribe via RSS Feed
Add Techgurulive to Google

When scheduling a cron job each column indicates a different time period:

minute hour  [Day of Month] [Month] [Day of Week]

Thus, to schedule a job to run at 5 minutes after every hour:

5 * * * * /job

every five minutes:

0-59/5 * * * * /job

some versions of cron allow the abbreviation: */5 * * * * /job

every hour on the hour:

0 * * * * /job

On the first of every month (at 1 am):

0 1 1 * * /job

Field Locations and Meaning:

  1. Minute (0-59)
  2. Hour (2-24)
  3. Day of month (1-31)
  4. Month (1-12, Jan, Feb, …)
  5. Day of week (0-6) 0 = Sunday, 1 = Monday, … or Sun, Mon, etc)
  6. Command to execute

Some versions of cron allow an extra field before the command to
specify the user that the command will run as.

35 2 * * * root tar czf /usr/local/backups/daily/abc.tar.gz /abc >/dev/null 2>&1

This will run tar czvf /usr/local/backups/daily/abc.tar.gz /abc at 2:35am every day.

The > /dev/null 2>&1 part means to send any standard output to /dev/null and to redirect standard error (2) to the same place as the standard output (1).

Basically it runs the command without any output to a terminal.

To view the cronjob currently on server use this command :

  1. crontab -l

To edit the cronjob on the server use this command :

  1. crontab -e

Once your cronjobs on the editor (by default it using “vi”) you can do the necessary amendments and save it by using the “vi” command :wq – which will overwrite current crontab settings.

Cronjob is very useful to schedule your requirements by setting up specific time to start, so no need hands on to execute the linux / unix command at time time.

eg : If you want to run a mysql database backup at the midnight from your server, can setup the cronjob like this :

0 0 * * * sh /root/backup/dbbackup

and the /root/backup/dbbackup contain the mysqldump command :

  1. !/bin/sh

cd /root/backup /usr/bin/mysqldump -uroot -ppassword –opt databasename > /root/backup/dbname.sql

So, every midnight, the cronjob automatically execute and do a mysql backup on /root/backup directory. But this method will always try to overwrite last night’s backup. If you want to backup everyday with a different name based on date, simply use this way :

  1. !/bin/sh

cd /root/backup egwdate=/root/backup/db_backup_$(date +%Y%m%d).sql /opt/mysql41/bin/mysqldump -uroot -ppassword –delayed-insert –add-drop-table databasename > $egwdate

The above command, please replace the username and password for the mysqldump, with your own user ame and password. This is just for your information only. As a result you should be the database backup name added with the data, which look something like this :

-rwxrw-rw- 1 users users :36 db_backup_ -rwxrw-rw- 1 users users :13 db_backup_

It will keep increment, until you manage it.


Tags:,

Written by Editor


Not Getting : Search to find what you are looking for


Liked this article? To continue getting our latest free Howtos and Tutorials,
you can also grab the RSS feed or Subscribe to Techgurulive by Email

Leave a Reply



This Howtos posted under : Home > Linux->

How to Schedule a Unix cron job