SaltyCrane Blog — Notes on JavaScript and web development

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 blank line 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.)
    (NOTE 3 (added 2015-06-24): When I created my cron script in /etc/cron.d with Emacs using sudo::, cron didn't pick up my script. When I created it with nano, cron picked it up. It seems the cause is the permissions of the cron script. Emacs created the script with 664 permissions while nano created the script with 644 permissions. When I changed the permissions to 644, it started working. I am running Ubuntu 15.04. This Ask Ubuntu answer confirms that a 644 permission is problematic because it is considered insecure. See /var/log/syslog for cron messages. The Ask Ubuntu page has a lot of other good tips: Reasons why crontab does not work)

  • 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

See also my post: Postgres backup with cron


#1 haimeika commented on :

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.

#2 Priyank commented on :


Thanks a lot for the notes.

#3 pax commented on :

Can you update this to show how to run a python script with a crontab? That would be helpful and even more applicable to your blog.

#4 JimTheMan commented on :

When I type the tail command I get this: tail: cannot open ‘/tmp/cron_output’ for reading: No such file or directory


#5 Arturo Sanchez commented on :

Hi Jim,
I guess you have to wait at least one minute until the cron job executes by first time and create the file.


#6 amm j commented on :

Hi, I am a beginner of linux and crontab. I follow your steps and I manage to get the expected result. Thank you so much.
But if I want to run php script with a crontab, how to do that ya? Inside the php script, I want to insert some data into our database.
I edit the crontab and write this: * * * * * /home/ubuntu/testfolder/testCron.php . But nothing inside my database. I have no idea about this. Really appreciate is someone can help me about this. Thank you.