Scheduling events with cron

It's convenient to be able to program the server to execute some tasks without our manually initiating them. One way to do this is by using cron, a daemon that runs on your system.

If you've written a program that can send you a text, let's have the server automatically run that program at 7am every morning. We'll have the server send us a "Wake up!" text.

  1. Use the Terminal to log on to your server
  2. Ensure that your server is using the current local time rather than UTC ("Coordinated Universal Time"). Instructions on how to do that are included here.
  3. Set up a crontab entry
    In the Terminal, type
    $ crontab -e
    ... and hit [Enter]. You'll be presented with some text on the screen which may not make much sense right away. That's okay. We're going to add an entry to this file that will cause the webpage to be opened at a specific time every day.

    Use the arrow keys to space down and add this line to the file at the bottom:
    59 06 * * 1-5 /usr/bin/python3 /home/ubuntu/Documents/pythonGmail/wakeup_text.py
    Note that we're using the full path to indicate which Python we want to use and where exactly the script is located. This is because cron may run as a different user with different defaults, so specifying absolute paths rather than relative paths will avoid any potential difficulties.

    What happens now? The crontab program runs in the background on your computer, and checks every minute or so to see if it should be doing anything. There are six fields in the line that you can specify: the minute, the hour, the day of the month, the month, the day of the week, and the command to be executed.

    The instruction to start the launcher using bash is the last entry. The five fields in front of that instruction indicate when the command should be run.

    From the Wikipedia entry on cron:

    # ┌───────────── min (0 - 59) # │ ┌────────────── hour (0 - 23) # │ │ ┌─────────────── day of month (1 - 31) # │ │ │ ┌──────────────── month (1 - 12) # │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0) # │ │ │ │ │ # │ │ │ │ │ # * * * * * command to execute

    The asterisk * indicates that the job should be run during every time period indicated. So, our launcher will run at 6:59 every day of the month, every month of the year, Monday through Friday.

    If you wanted the script to run only once a year, on November 21 at 10am:

    00 10 11 21 * /usr/bin/python3 /home/ubuntu/Documents/pythonGmail/wakeup_text.py
  4. Save the entry
    Press the "control" key and the letter "x" to exit crontab. You'll be asked if you want to save the file—say "Y"—and then what filename to save it under: just hit [Enter] to accept the default value.
  5. Wait until 6:59 on a weekday morning and see what happens!
    If your computer is closed or sleeping, the command should be executed when you wake up the machine.

How do I remove the crontab entry?

To remove a crontab entry from the server:

  1. Open up a Terminal window and log on to your server
  2. Type
    $ crontab -e
  3. Use the arrow keys to space down and delete the line that runs the script.
  4. Save the entry by pressing the "control" key and the letter "x" to exit crontab. You'll be asked if you want to save the file—say "Y"—and then what filename to save it under: just hit [Enter] to accept the default value.