Welcome, Guest. [ Log In ]
Question   How do I set up a cron job?
Search KBase

Top 4 in this Area:
1. How do I set up a cron job?
2. What is a cron job?
3. May I run a cron job?
4. What is your persistent (background) process policy?

How do I set up a cron job?
Via the "crontab" command from the shell.

crontab -l will show you your currently set up cron jobs on the server.

crontab -r will delete your current cron jobs.

crontab -e will allow you to add or edit your current cron jobs by using your default text editor to edit your "crontab file".

Note, vi has a bug in it and will not work to edit your crontab! We recommend nano (which is just like pico), which is the default editor anyway!

Your crontab includes all the cron jobs you'd like, with one cron entry per line. A cron entry looks like this:

45 2 * * *     /home/user/script.pl

  • The first number is the minute of the hour for the command to run on.
  • The second number is the hour of the day for the command to run on.
  • The third number is the day of the month for the command to run on.
  • The fourth number is the month of the year for the command to run on.
  • The fifth number is the day of the week for the command to run on.

    Here are some examples to help you learn the syntax for the numbers:

  • 32 * * * * : will be run every hour on the 32nd minute.
  • 12,42 * * * * : will be run twice an hour on the 12th and 42nd minutes.
  • */15 */2 * * *: will be run at 0:00, 0:15, 0:30, 0:45, 2:00, 2:15, 2:30, ...
  • 43 18 * * 7: will be run at 6:43pm every Sunday.

    Last updated: Apr 26, 2004.

  • Official Reply (2004-04-26 10:16:43 )
    If you use the default Borne Again SHell, you can specify your default text editor by entering
    export EDITOR=joe
    on the command line. Substitute the command to invoke your favorite editor where "joe" appears.

    There actually *is* an editor named joe. It's a WordStar clone. I've never devoted 27 years of my life to learning vi or "Eight Megs And Constantly Swapping", but all old geeks know WordStar, don't they?
    Official Reply (2004-04-26 10:16:29 )
    You can also edit crontab on a local file. Upload it to a directory and run:

    crontab YourFileName

    It'll replace your crontab with what's on the text file, without having to edit it online. This is useful if you're using a telnet client which doesn't support ANSI control codes (such as the Windows' one) and can't understand what the heck is going on on the editors.
    Official Reply (2004-04-26 10:15:20 )
    To change your default editor do:

    export EDITOR=nano
    (bash, zsh, ksh)
    setenv EDITOR nano
    (tcsh, csh)

    Should probably add this to the official part, but you probably also want to add:

    > /dev/null

    at the end of a command so that stdout doesn't get emailed to you. If you don't want stderr either, do:

    > /dev/null 2>&1

    You might consider sending stdout and stderr to logfiles instead of to the bit bucket.

    If you want to avoid cron related messages entirely, put:


    at the top.

    Another useful hint - the man page for crontab that you probably want is in section 5, so to find out the syntax of a crontab itself (not the usage of the command "crontab", type
    $ man 5 crontab
    User Post (2006-01-07 18:15:53 by bhaan)
    I am using a windows computer with a telnet program called puTTY and i wrote a text file with five lines of crons and tried to run it and get the error that i cant install. I also used nano through puTTY to write it through there but when i say to write out and then exit it does not write anything to the file. Any suggestions would greatly help.
    User Post (2006-01-05 17:50:39 by embeddedrf)
    While trying to use Windows TelNet to install the cron.job file I keep getting "errors in crontab file, can't install. I have a single line crontab file that I have 1) set the attributes on 2) checked the syntax on 3) diddle the single crontab file with regards to cr or \n at the end of the single line and still I can not get past the error. Anybody have a suggestion or 3 ?
    User Post (2006-01-03 14:19:53 by bhershberg)
    Each cron job must be contained on a signle line. The nano editor appears to justify text by default, which will cause crontab to bail on statements that end up justified as several lines. The ^J command in nano will turn off justification and allow you to create very long cron job statements that stay on a single line.
    User Post (2005-10-14 04:01:40 by walterhutchens)
    Thanks for this info, all. I am a command-line-phobic WordPress blogger, but with this info I was (with effort) able to create a cron file to work with the Postie plugin for culling email for posts. Many thanks.
    User Post (2005-09-02 04:10:21 by dodald)
    FYI: Some people are reporting that VI works with crontab, and others are reporting that it doesn't... It is acually VIM that does not work, by default, because of a security bugfix but there is a workaround.

    The Workaround: (for people who hate joe/pico/...:))
    "set backupcopy=yes"
    to your .exrc or .vimrc
    User Post (2005-07-09 15:52:35 by lbstoldt)
    If your command is long and you're editing your crontab using nano, you'll probably need to turn off auto-wrap (this is probably alt-W on your keyboard)

    Also, if you compiled your own version of php, you can use that in your php file instead of usr/local/bin/php, for example if you compiled your php to a directory called "php"...

    User Post (2005-03-14 17:05:28 by vajonez)
    "Note, vi has a bug in it and will not work to edit your crontab!"

    Perhaps this information is out of date, but vi works just fine for editting my crontab.
    User Post (2005-02-25 01:32:15 by webweber)
    When using a textfile it is essential to upload it as ASCII (_not_ Binary) and to terminate the cron job commands with a line break. See comments in this sample .txt file:

    # This text file must be uploaded to the server as ASCII file, check the settings of your ftp client.
    # It won't work when uploaded as Binary - 'bad day-of-month', 'file not found' and other nonsense will be the result
    # The file permission (chmod) of the php script that is called must be 'execute'
    # There are 2 ways to execute a php script as a cron job - but in any case the php interpreter must be called first:
    # 1) if the php file contains a 'shebang' that first calls the php interpreter
    # ('shebang' : #!/usr/local/bin/php - on first line, before opening '<?' tag),
    # the cron job can call the file directly:
    * * * * * /home/someDoma/someDomain.com/withSheBang.php
    # 2) if the php file does not contain a 'shebang', call the php interpreter first, pass the path
    # to the php file as a parameter - note the space after the first 'php'
    * * * * * /usr/local/bin/php /home/someDoma/someDomain.com/withoutSheBang.php

    # (every cron job must be terminated with a line break, otherwise it is ignored)

    (watch for unwanted line-breaks in the sample above - every line must either start with a # or a *)
    User Post (2005-02-22 15:18:27 by dpmeyer)
    rosborne, using lynx for this is a somewhat bad idea, as you're relying on a number of things being in place at all times. You're also doing this:


    Much better to follow the other suggestions to call php directly. You can find out where php is by typing 'which php' at a command line. That results in:


    Which is much better.

    0 0 * * * /path/to/php /path/to/your/script.php
    User Post (2005-02-07 16:14:19 by anarax)
    using the php CLI (command line interface) consumes less resources than using lynx to execute php code

    You can add "#!/usr/local/bin/php" as the first line of your php file, chmod it executable (chmod +x foo.php) and then just run it in your cron job:

    0 * * * * /home/user/foo.php

    and if you don't want to do all that you can just point php to your script

    0 * * * * /usr/local/bin/php /home/user/foo.php
    User Post (2004-12-17 21:30:37 by kcarlson)
    Thanks to rosborne for the idea of using lynx to run a
    PHP cron job! One concern might be the risk of bots or spidering scripts (such as Teleport Pro) running the script at inappropriate times.

    I haven't tried this yet, but perhaps adding a variable declaration to the URL address, and then halting the script if the variable doesn't exist, would address this concern.
    i.e. http://www.yourdomain.com/cronfile.php?run_me=YES

    User Post (2004-07-08 17:15:02 by deanpence)
    To only temporarily use nano to edit your crontab:

    echo "alias crontab 'EDITOR=nano crontab'" >> ~/.profile

    and log back in.

    cron will send you any output that anything in your crontab produces. This is usually a good thing! But if you don't want the output mailed to you for some script, do something like this:

    0 * * * * your-command-here > /dev/null 2>&1

    The part starting with the ">" (greater-than sign) makes all the output go into oblivion, so you'll never, ever get any output from the cron job, even when it fails (although cron should notify you when it fails).
    User Post (2004-05-21 02:29:11 by murrayc)
    Does anyone know how to stop if from sending the results as an email each time?
    User Post (2004-05-14 16:20:39 by libcrypt)
    I recommend,

    alias crontab 'setenv EDITOR ed; /usr/bin/crontab \!* ; setenv EDITOR vi'

    If you don't like ed, then substitute yr fave editor.
    User Post (2003-07-30 08:03:17 by rosborne)
    If you are trying to set up a cron job, and you already have the code written in PHP - an easy way to do this is by using lynx. By doing this, it is like setting the server to open a webpage as the cron event...just makes it easy to use PHP as your scripting language.

    ie. of cron entry:<br>
    0 0 * * * lynx -dump http://www.yourdomain.com/cronfile.php<br>
    This would run the cron event at midnight...works like a charm.
    User Post (2003-05-06 19:17:20 by ddyer-bennet)
    Sorry, county, WordStar is *not* a geek editor; it's from the CP/M world, not a geeky kinda place at all. :-)

    Not that I have much against joe, mind you.