Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

Simple cron example

Simple cron example (tested on Ubuntu):

  • Edit your (user) crontab file
    $ crontab -e
    This will bring up your editor (nano by default in Ubuntu)

  • Enter the following inside. This will append the current date to a log file every minute. The 6 fields of the crontab file are: minute, hour, day of month, month, day of week, command.
    * * * * * /bin/date >> /tmp/cron_output
    
    Be sure to put a newline at the end of the file.
    (NOTE 1: >> only redirects STDOUT to a file. To redirect both STDOUT and STDERR, use something like /bin/date >> /tmp/cron_output 2>&1)
    (NOTE 2: If output is not redirected, cron will try to email the output to you. To do this, a mail transfer agent such as sendmail or postfix must be installed.)

  • Exit the editor. It should output:
    crontab: installing new crontab
  • Check that it is working:
    tail -f /tmp/cron_output
    You should see the date updated every minute on the minute (or close to it):
    Tue Sep 16 23:58:01 PDT 2008
    Tue Sep 16 23:59:01 PDT 2008
    Wed Sep 17 00:00:01 PDT 2008
    Wed Sep 17 00:01:01 PDT 2008
    ...
    

1 Comment — feed icon Comments feed for this post


#1 haimeika commented on 2009-06-29:

Hi, Sofeng

I am a beginner of linux. The example is very simple but you list the steps very clearly. It is so helpful for me, and thank you very much.

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Slicehost