Wednesday, September 1, 2010

crontab stuff

Run crontab Every 10 Minutes

by Vivek Gite · 3 comments

Q. How do I run a cron job or a shell script every 10 minutes using Linux / UNIX cron service?

A. cron is a time-based scheduling service in Linux / Unix-like computer operating systems.

Login to UNIX system

Type the following command to enter cronjob:
$ crontab -e
Each cronjob has following syntax:

# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
* * * * * command to be executed

To get crontab to run a task every 10 minutes you could type as follow
*/10 * * * * /path/to/command
Save and close the file.

credits to cyberciti.biz

1 comment:

  1. Examples

    How do I run a task every 5 minutes?

    One option is to use

    MAILTO=cron@username.plus.com
    0,5,10,15,20,25,30,35,40,45,50,55 * * * * /command/to/execute

    However, there is a special shortcut for this:
    MAILTO=cron@username.plus.com
    */5 * * * * /command/to/execute

    The */5 is known as a short form equivalent to 0,5,10,15,20 etc... and achieves the same effect as the previous example, executing the command every 5 minutes. Other examples are: */2 would be every 2 mins, */30 every 30 minutes and so on. You can use the same short form for the hour indicator */2 every 2 hours, */6 every 6 hours etc.

    How do I run a task at 6PM every night?

    MAILTO=cron@username.plus.com
    00 18 * * * /command/to/execute


    How do I run a php script at 2am every Sunday?

    MAILTO=cron@username.plus.com
    00 02 * * sun /usr/local/bin/php $HOME/php/script.php

    Notice that the php command is specified using an absolute path because cron will not be able to find it otherwise. If it was not specified, the script.php will fail to execute and an error like: php not found will be reported in the email you receive or in Mailbox. You may not spot this, especially if you run it successfully from the $ prompt as php /absolute/path/to/script.php or even php script.php if it is in the current directory. Also note $HOME is being used instead of /files/homeX/username/

    Example MailBox or Email from cron

    tutorialsteam@shell2 tutorialsteam $ more Mailbox
    From root@shell1.cgi.plus.net Sun Feb 01 23:11:27 2004
    Return-Path:
    Delivered-To: tutorialsteam@shell1.cgi.plus.net
    Received: (qmail 18527 invoked by uid 10667); 1 Feb 2004 23:11:25 -0000
    Date: 1 Feb 2004 23:11:25 -0000
    Message-ID: <20040201231125.18514.qmail@shell1.cgi.plus.net>
    From: root@shell1.cgi.plus.net (Cron Daemon)
    To: tutorialsteam@shell1.cgi.plus.net
    Subject: Cron $HOME/me
    X-Cron-Env:
    X-Cron-Env:
    X-Cron-Env:
    X-Cron-Env:

    I am:
    uid=10667(tutorialsteam) gid=500(shellcgi) groups=500(shellcgi)
    PATH is set to:
    /usr/bin:/bin


    Crontab command options

    crontab -e

    As explained earlier, this will allow you to edit the contents of your crontab file or create a new crontab file if one does not already exist. The editor used is called vi or vim.

    crontab -l

    This will list the current contents of your crontab file and is very useful for checking you have edited it correctly after crontab -e. It is often useful to make a copy of the crontab file in case you make a mistake with an edit.
    $ crontab -l >mycrontab

    This will create a local copy of the crontab file called mycrontab.

    crontab -r

    Use with caution: This will delete the contents of your current crontab file (another reason for making a local copy!)

    crontab file

    This is an alternative method for setting up your crontab file. Instead of using crontab -e, you can create a file containing the cron commands and use that to replace or overwrite the current contents of your crontab file. Note replace - it will overwrite anything that is currently in your crontab file with the contents of file.

    ReplyDelete