Bobcares

An introduction to cron and its administration from command line interface

by | Sep 1, 2013

A cron job is the first bit of magic a new webmaster would see in a web hosting account. You tell the server to run a script at scheduled times, and it gets done. Updates happen automatically, data gets filled in automatically, and mails get sent right on schedule. Its like you get your personal robo-servant.

All the modern web hosting control panels like cPanel have beautiful interfaces to add cron jobs as is shown in this WHMCS documentation, and most webmasters will be just happy to use that and be done with it.

However, if you are one of those technology loving webmasters who dream to own a VPS or a dedicated server at some time, and want to know the internal workings of the cron magic, read on.

So, how does cron happen?

Its simple really. A program called “crond” always runs in your server, and every minute it looks through the task lists to see if something needs to be executed at that minute. If there’s a job to be done, it is executed, and results are sent to the contact address of the owner.

How is cron controlled?

The cron program is controlled through configuration files at THREE primary locations

  1. /etc/crontab – This is the main control file of cron daemon. This file just have basic program variable definitions, and control entries for cron jobs elsewhere. The important thing to note is – do not edit this file unless you do not have a very good reason to do so.
  2. /etc/cron.d – Unlike /etc/crontab, this is a directory. This directory, along with /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly, provides a place for APPLICATIONS to put their scheduled tasks. Again, ideally you should leave these folders to itself, unless you have have to put some scripts in them as part of an application installation.
  3. /var/spool/cron – THIS is the most important folder for our requirements. In this directory, a file is created for each user of the server, and cron tasks are added one line after another. It will look something like below:

 

MAILTO="me@bobcares.com"
5 0 24,25 * * /usr/bin/php /home/mydom/public_html/runscan.php
45 0 24,25 * * /usr/bin/php /home/mydom/public_html/mydomain.com/runscan.php
50 0 24,25 * * /usr/bin/php /home/mydom/public_html/myotherdomain/runscan.php
0 0 20 * * /usr/bin/php /home/mydom/public_html/runspider.php
50 0 20 * * /usr/bin/php /home/mydom/public_html/mydomain.com/runspider.php
0 0 23,24 * * /usr/bin/php /home/mydom/public_html/myotherdomain/runspider.php

 

Looks complex, doesnt it? This leads us to the next question. How do I define a cron command?

What is the format of a cron entry?

The format of a cron job is most easily illustrated using the image below:

Format of a cron job entry

Format of a cron job entry

The image is self-explanatory. A cron entry has the first 5 positions reserved for defining the time to execute the task, and the last position reserved for the task command. Optional arguments to the command may follow the definition.

The few examples below will quickly get you the hang of it.

 

MAILTO="me@bobcares.com"
07 * * * * command "some args" - This command is run at seven minutes past every hour
23 10 * * * command "some args" - This command is run daily at 10:23 am
23 22 * * * command "some args" - This command is run daily at 10:23 pm
00 9 * * 2 command "some args" - This command is run at 9 am every Tuesday
* 10 * * Tue command "some args" - This command is run at 10 am every Tuesday
23 10 1 * * command "some args" - This command is run 10:23 am every 1st of the month
*/5 8,18 * * * command "some args" - This command is run every 5 minutes of hours 8:00 am and 6:00 pm
01 * 15,25 02 * command "some args" - This command is run one minute past every hour on the 15th and 25th of February

 

Now, we are ready to create our own cron entry from command line interface.

How do I create a cron from command line interface?

If you have a command line interface for your server, the command you will need to use is:

 

cron -u <myusername> -e

 

This will then open an edit window for the cron entries for the username “myusername”. You can then define the periodicity and command as explained in the session above. Put your email ID in the MAILTO variable to send you cron updates, and you are done! Save the file and exit. Cron daemon will automatically detect the changes and schedule it for the next run.

Common pitfalls in getting a cron to work

I would recommend that you first run the proposed command with the time format “* * * * *“, and check for updates in /var/log/cron. If you see your command getting executed, you know your command works in the cron environment. If not, check for the following 3 situations:

  1. Does your script or command have 755 permissions, and the directory accessible? – The vast majority of issues are due to the script not having the right permissions or the directory path not having the right permissions for the user to access.
  2. Have you given the absolute path to the script/command in the cron job? – Cron daemon unable to locate your command/script has resulted in far too many errors. Just give the absolute path to the script/command. Its a good practice.
  3. Did you confirm that the cron daemon is indeed running? – If the /var/log/cron log file has not shown any new output in a while, it would be prudent to check the status of the cron daemon using the command /etc/init.d/crond status.

Here ends our quick introduction to command line administration of cron. Leave your comments at our page for any clarfications or feedbacks.

 


About the author
is a senior software engineer at Bobcares. He has extensive experience in managing technical support teams of web hosting companies and data centers. He is passionate about systems engineering, and loves to get his hands dirty on systems automation. His free time is spent reading books and being with his family.


0 Comments

Never again lose customers to poor
server speed! Let us help you.