Unix : Cron daemon, crontabs and where to find them

Cron is a fairly standard daemon which you’ll find on most (if not all) Unix machines. Its purpose is to schedule the execution of commands at a specified time.

Sometime you’ll log at some performance graph (CPU load for example) and find out that every day/week/month/other there is an unexpected peak and you’d like to know why. Of course if this is regular you’ll think of Cron as a good trail to follow.

Configuration files for those scheduled jobs are stored in files which are called “crontabs”. Usually, a man crontab will give you the format of this file.

Now the next question you’ll ask is “where to find all the scheduled jobs ?”

The answer varies greatly following the Unix flavor your using, as well as the cron version or clone is running on the system.

Standard places are :

  • /var/spool/cron : in this directory you’ll find a file for each user who has cron scheduled jobs
  • /etc/crontab : this is a system global crontab. Sometimes this file will reference other directories (or files, depending on the clone of Cron) such as :
    • /etc/cron.hourly : hourly jobs, usually running at the start of a new hour
    • /etc/cron.daily : daily jobs, usually running during the night
    • /etc/cron.weekly : weekly jobs, usually running the night between saturday and sunday
    • /etc/cron.montly : montly jobs, usually running on the 1st of the month
  • Maybe your Cron clone will also allow you to have an /etc/cron.d directory where you’ll find application specific crontabs (for exemple a database might install there a crontab to schedule tables optimizations)

The usual syntax of a crontab is :
* * * * * user command-to-run

The 5 first fields (stars in this example) are for :

  1. minute
  2. hour
  3. day of month
  4. month
  5. day of week

If you put a star, then it means “every”, the example means every minute of every day of every month… you get it ;-). You can put multiple values separated by a coma.

If the minute field was “0,30” then the job would run at those minutes.

Some versions of Cron will allow you to write “*/N” meaning every N minutes / hours / whatever.

You can usually list cron jobs for a user by issuing a crontab -l user (yourself if you don’t specify a username) or edit them with crontab -e user (same remark applies).

There is much more to tell about cron (like about the output of the script being emaild, environment variables and so on), but this would go further than this introductory post … refer to man cron and man crontab to get all the gory details 🙂