× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Scheduling a file to run on OSX or Linux using crontab

Jessamyn Smith
Jun 11, 2015
<p>Let's say you have a handy little python script that checks a webpage for you, and you want to run it once a day. You could do this manually, but it's much nicer to have the computer do it for you.</p> <ol> <li>Make sure your script has executable permissions. In the OSX Terminal or Linux command line, go to the directory where your script is saved, and make it executable as follows (filename.py is the name of your script): <p>chmod u+x filename.py</p> </li> <li>Make sure your script is runnable from the command line. This generally involves adding a shebang as the first line of the script. For python, a typical shebang would be: <p>#! /usr/bin/python</p> </li> <li>Verify that the script now runs by putting the full path plus filename directly into the Terminal/command line, e.g.: <p>/Users/jessamyn/Development/filename.py</p> </li> <li>Add a cron job to run the script. You can edit crons for the current user with: <p>crontab -e</p> </li> <li>This opens the file in vim. Press 'i' to get into edit mode, and add a line for your script. E.g. to run once a day at 9:30pm: <p>30 21 * * * /Users/jessamyn/Development/filename.py</p> </li> <li>To save your changes, first press 'esc' to get out of edit mode, then type ":wq" to save and quit.</li> <li>Wait and watch, your script should now execute! (To verify this, you may want to make the crontab job for 2 minutes in the future, so you can see it run.)</li> </ol>
comments powered by Disqus