Automate running a script using crontab

Jessica YungProgramming

Problem: You want to run a script once every 5 minutes or at some other regular interval, but don’t want to do it manually.

You will need:

  • Access to your UNIX (Any *nix should work) shell
  • A script you want to run. If you don’t have a script you want to run, you can follow along with <a href="https://github.com/jessicayung/blog-code-snippets/blob/master/record_time.py" target="_blank">record_time.py</a> that appends a timestamp to a text file.

Optional Background: Cron is a tool in Unix that allows tasks to be automatically run in the background at regular intervals. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

Four (three if you’re lucky) Simple Steps:

Step 1: Go to your crontab file.

  • Go to Terminal / your command line interface. It might look like this:
  • Type crontab -e. The flag -e stands for edit. If your crontab file does not yet exist, this command will create the file for you.
  • Optional: If you want you can choose your own editor. I like nano, so I typed  export EDITOR=nano; crontab -e. All this is doing is specifying a code editor by setting the variable EDITOR to equal nano.
  • You should see something like this:
    • crontab file in nano editor

      crontab file in nano editor (I happened to open this in the mail directory)

Step 2: Write your cron command.

  • A Cron command first specifies (1) the interval at which you want to run the script followed by (2) the command to execute. The exact syntax is given below:
    • Example: If I want to run the python file hello.py on the first minute of every hour (00:01, 01:01, …, 23:01), I type this on the first empty line I see:
    • python ~/hello.py is what I would typically type in the console to run the script. (Format: $PYTHONPATH $FILEPATH.)
      • If you were in the same directory (folder) as hello.py, you might just type python hello.py in the console to run the script. But because the cron file is not necessarily in the same directory as your script, we need to be more specific about where the file is by specifying the full file path.
  •  Save and exit the file.
    • In nano, you can type ^O (control-o) to write the file, i.e. save changes.
    • Then type ^X (control-x) to exit the file.

Step 3: Check that the cron command is working.

  • If it’s not running, you’ll get an error in the file var/mail/$USERNAME.
    • You can access this file by typing
    • Note: Don’t type what you see before the dollar sign! And replace $USERNAME with your username, which is usually the word before the @ sign. E.g. my username is jessica. You can also type ls to list all the files in that directory. and then type nano $FILENAME to access that mail file.
    • You’ll find error logs inside the mail file that can tell you more about what went wrong.

Step 4: Debugging potential problems

  1. Python path
    1. Some modules such as  urllib.request are only available in Python 3+. Your default Python installation may be e.g. Python 2.7. So you may have to specify your (different) Python path. I use the Anaconda installation and my most up-to-date installation is at ~/Users/jessica/anaconda/bin/python, so I wrote my command as
    2. It may also be useful to add the Python path as the first line of your Python script like so:
      The equivalent first line for most Python installations is #!/usr/bin/python .
  2. Crontab Restrictions: User does not have permission to execute crontab
    1. I did not encounter this on my laptop, but this may be an issue especially if you’re trying this on a machine at work. As written in the Crontab Quick Reference, you should check if your username appears in the file  /usr/lib/cron/cron.allow . If the file does not exist, check if your username appears in the file  /usr/lib/cron/cron.deny . You want your username to be in the cron.allow file and not in the cron.deny file. Only one username should be listed on each line. If neither file exists, then only the root user can execute cron.

Further reading:

Crontab Quick Reference