Linux: Scheduled Jobs With crontab

Today I learned about crontab, which for those of us coming from the Windows world is the equivalent of the Windows Task Scheduler. If I had known how easy it was to use I would have started using it a long time ago.

HowToGeek has a great detailed write-up on it including plenty of examples of using the cron pattern. What I wanted to share was how to use I/O Redirection to update the file instead of using the crontab editor (crontab -e) as described in the HowToGeek article.

I wanted to setup a really simple file backup to Amazon S3 on an Amazon EC2 instance. I also wanted to make sure I didn’t have to set it up more than once. So I needed something that would work well as an EC2 User Data script:

First, copy crontab to a temp file named ‘cron-temp’ (arbitrary name):

$ crontab -l > cron-temp

Next, use the append output-redirector >> to write a string to the temp file named 'cron-temp’. If I wanted to overwrite the file completely I could use > instead:

$ echo '[minutes] [hours] [days] [months] [day-of-week] [bash command or script file]' >> cron-temp

In my case I wanted to run the script at the top of every hour so the script actually looked like this see the HowToGeek article for details on cron patterns:

$ echo '0 * * * * aws s3 cp [local file path] s3://[s3-bucket-path]' >> cron-temp

Now that we’ve updated the temp file replace crontab file with contents of the temp file:

$ crontab cron-temp

Finally, delete the temp file:

$ rm cron-temp