How to use Cron TABle (Dreamhost.com)
Written by Administrator   
Wednesday, 06 February 2008

Cron is a unix utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon.

Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

How to use Cron (Dreamhost.com

  • You'll need to enable shell access (SSH) before you can modify your crontab
  • Crontab file: Create and Edit
  • Generate log file
  • Example
  • Run php from cron


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

Enable shell access (Dreamhost.com )

  1. Visit the Manage Users (https://panel.dreamhost.com/index.cgi?tree=users.users& ) page of the control panel.
  2. Click the [ edit ] link located next to the user you'd like to enable shell access for.
  3. Check the 'Enable ssh/telnet?' box for that user.
  4. Click 'Save Changes' to save the change.
  5. Wait about 20 minutes for this change to take effect.

 

Crontab file: Create and Edit

crontab -e     Edit your crontab file, or create one if it doesn't already exist.
crontab -l      Display your crontab file.
crontab -r      Remove your crontab file.
MAILTO=
 This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
 
# minute (0-59),
# | hour (0-23),
# | | day of the month (1-31),
# | | | month of the year (1-12),
# | | | | day of the week (0-6 with 0=Sunday).
# | | | | | commands
3 2 * * 0,6 /some/command/to/run
3 2 * * 1-5 /another/command/to/run

 DreamHost cron does NOT have an environment. The following will not work!

* * * * * perl myscript.pl

* * * * * perl /home/myuser/mydomain.com/myscript.pl

 

Generate log file

 The output from commands is determined by the last entries on the line. If you don't specify anything at all, regular and error output will be e-mailed to you.

  • MAILTO environment variable in the crontab If MAILTO is defined (and non-empty), mail is sent to the user so named. If MAILTO is defined but empty (MAILTO=""), no mail will be sent:
    MAILTO=
     This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
       
  • Email you the regular and error output:
    8 2 * * * /home/you/bin/coolscript.pl
  • Only email you errors: 
    8 2 * * * /home/you/bin/coolscript.pl >/dev/null
  • Not tell you anything (+ disable E-mail):
    8 2 * * * /home/you/bin/coolscript.pl >/dev/null 2>&1
  • log in file:
    8 2 * * * /home/you/bin/coolscript.pl > /home/you/cronlogs/you_log_cron.log

 

Example

  •  Run a script every 20 minutes:
*/20 * * * * /usr/local/bin/perl /home/you/mydomain.com/myscript.pl
  • Run a php page called cron.php every hour (use wget):
0 * * * * wget http://www.somedomain.com/cron.php
  • Run a command at 2:08am 
8 2 * * * /home/someone/bin/coolscript.pl
  • Run a command at 2:00am on saturday
0 2 * * 6 /home/you/weekly/weekly.pl
 

Execute php using cron

  • To execute a PHP script using crontabs, you must add this line to the top of the PHP script you want to execute for it to work:
#!/usr/local/bin/php -q
  • Alternately, you can set up your cron entry like this:
* * * * * /dh/cgi-system/php.cgi /home/youruser/yourdomain.com/path-to-php4-script.php
* * * * * /dh/cgi-system/php5.cgi /home/youruser/yourdomain.com/path-to-php5-script.php

 

Last Updated ( Wednesday, 06 February 2008 )