Codular

HomeWriters RSS

Adding Jobs to the Crontab

Introduction

Utilising something called the crontab, we can schedule jobs, or cronjobs, to run at a set time or date. Really useful for things like updating a cache of data, or sending out tweets announcing a random - it is used on the Codular servers for that reason.

How?

With some hosting providers, you may find yourself provided with a nice clean GUI through which you can select intervals, and commands to execute. If this is what you have, it's probably best to use that. However, if you don't have that, and you have SSH access to your server, you should be able to carry on as below.

There is one command we're going to use when SSHed into the server, but passing two different flags each time. One allows us to view the current jobs that are scheduled, and the other allows us to edit the crontab and add/remove/edit jobs.

To view the list of jobs use: crontab -l

To edit the list of jobs use: crontab -e

When editing the list, the default editor will be used - this will most probably either be nano or vim. As soon as you quit from editing the crontab, the changes that you made will come into effect.

Format

One thing will be immediately clear if you have some jobs in your list already, there are a lot of numbers, and asterisks (and in some cases forward slashes) at the start of each line. These are there to identify the interval at which the job should be ran.

You can use the handy little template below at the top of your crontab to help you easily, and quickly decode what they mean.

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  *  command to be executed

As you can see from the above template, there is a section for the command to be ran. This could be anything from a PHP command, all the way through to a complex BASH script if you wanted.

We have a job running that will publish articles on a periodic basis, as well as tweeting the link out every 2 days. Below are examples for a command that would run at regular intervals:

Every 5 minutes:

*/5 * * * * php /mytestphpscript.php

Every 2 days at 1pm:

0 13 */2 * * php /mytestphpscript.php

If the first two values had been *, this would run every minute, every hour on every 2nd day. It's best to ensure you restrict the time that the jobs run.

Debugging

Any output that is sent form the cronjob will by default not have anything done to it. It may email you depending on your config, however, in a lot of cases this won't happen.

You can direct the output of a cronjob to a file by using the right arrow > with a file name after it. For example:

*/5 * * * * php /mytestphpscript.php > /cronlog.txt

Conclusion

A very brief intro to crons, and how to create them. The template above is a very useful tool for helping format, and construct your cronjobs correctly.

If in doubt, it's advisable to be conservative on the timing of the cron and tweak the timing when you are sure it's not running too frequently. Running something every minute, may lead to you overloading your server if you're not careful.