The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Cron Help

Discussion in 'General Discussion' started by gopal, Sep 4, 2004.

  1. gopal

    gopal Guest

    Field Meaning
    1 Minute (0-59)
    2 Hour (2-24)
    3 Day of month (1-31)
    4 Month (1-12, Jan, Feb, etc)
    5 Day of week (0-6) 0 = Sunday, 1 = Monday etc or Sun, Mon, etc)
    6 User that the command will run as
    7 Command to execute

    So using one of our original examples:

    12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1

    This will run tar czvf /usr/local/backups/daily/etc.tar.gz /etc at 3:12am every day. The >> /dev/null 2>&1 part means to send any standard output to /dev/null (the linux trash can) 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 etc.

    Another example of a more complex entry would be:

    30 15 13 6 1 * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1

    This will run tar czvf /usr/local/backups/daily/etc.tar.gz /etc on Monday June 13th at 3:30pm
    You could also use the following to achieve the same results

    30 15 13 Jun Mon * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1

    If you wanted to run a command as user joey 15 minutes after every hour regardless of the date you could add the following entry:

    15 * * * * joey /usr/bin/somecommand >> /dev/null 2>&1


    The astrix '*' in the example above is a wildcard meaning that cron will ignore the field.

    If you wanted to run a command every 2 hours you could enter in */2 for the hour field. This would run the specified command at 2am, 4am, 6am, 8am, 10am, 12pm, 2pm, and so on. An example of this type of entry would be:

    0 */2 * * * joey /usr/bin/somecommand >> /dev/null 2>&1

    You can also use commas to specify more than one time per entry. For instance if you wanted to run a command at 15 and 30 past each hour you would enter in 15,30 for the minute field. An example of this type of entry would be:

    15,30 * * * * joey /usr/bin/somecommand >> /dev/null 2>&1

    If you wanted to run a command every day at a certain time for the first week of the month you would enter in 1-7 for the day field. An example of this type of entry would be:

    15,30 */2 1-7 * * joey /usr/bin/somecommand >> /dev/null 2>&1

    This would run somecommand every 2 hours at the 15's and 30's (2:15, 2:30, 4:15, 4:30 etc) for the first 7 days of the month.

    If you want cron to execute a bunch of scripts at 4:18pm every day you could put all of the scripts in one directory (for example, /home/username/cron) and add the following line to your crontab:

    18 16 * * * root run-parts /home/username/cron >> /dev/null 2>&1

    If you wanted to save the output of a certain command you can replace the >> /dev/null 2>&1 with >> /home/user/somecommand.log 2>&1

    After you've added all your entries you can use the command crontab -l to list them.

    If you wanted to remove your crontab file you could run crontab -r to delete it.

    To edit a users crontab file as root you can run crontab -e -u username

    So as you can see, Cron is very configurable and is a great tool for every system administrator to automate tasks.
     
Loading...

Share This Page