January 24, 2012 Mark Bulling

EC2 Tutorials: Scheduling tasks on EC2 using Crontab

Tweet about this on TwitterShare on LinkedInShare on FacebookGoogle+Share on StumbleUponEmail to someone

One of my main reasons for wanting an EC2 instance was to be able to automatically run scripts at certain times, normally to collect data and save it to a database. As my EC2 instance is always running, I can forget about it for a month and have a month’s worth of data ready and waiting.

Want to schedule scripts so that they automatically run at set times or at set intervals (e.g. every day at 1pm or every 10 minutes)? No problem, Crontab is the easy to use Linux scheduling program that allows such execution in a no-thrills, straightforward way.

The syntax works along the lines of:

<minute> <hour> <day of month> <month> <weekday> <task>

For example:

0 2 * * * python /location/of/script.py

will run script.py at 2am every day. Simples. (* is essentially a placeholder to say “do it for all integer values”)

Lots more detail can be found here, here and here.

To create or access your crontab, type crontab -e on the EC2 command line.

To see what’s scheduled by crontab, type crontab -l on the EC2 command line.

Leave a Reply

Your email address will not be published. Required fields are marked *

Machine Learning and Analytics based in London, UK