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
1jessica@comp:~$ export EDITOR=nano; crontab -e
- You should see something like this:
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:
12345678* * * * * 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)
- 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:
11 * * * * python ~/hello.py
python ~/hello.py is what I would typically type in the console to run the script. (Format:
- 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
- You can access this file by typing
123jessica@comp:~$ cd ../..jessica@comp:~$ cd var/mailjessica@comp:~$ nano $USERNAME
- 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.
- You can access this file by typing
Step 4: Debugging potential problems
- Python path
- 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
11 * * * * * ~/Users/jessica/anaconda/bin/python ~/hello.py
- It may also be useful to add the Python path as the first line of your Python script like so:
123#!/Users/jessica/anaconda/bin/python# Your Python script hereprint("Hello World")
- 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
- Crontab Restrictions: User does not have permission to execute crontab
- 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.