Browse By

Discover the Power of Cron Jobs

crontab

Cron jobs are commands run by the unix program cron. The cron program takes a specific syntax and runs the command at every time interval set by the user. These are used to automate scripts of all kinds. There are a lot of uses out there for setting up crons with web applications. As webmasters, if we can harness the power of the cron application, we can use cron jobs to our advantage in a number of ways. Let’s get right into it.

  • How do you set up a cron job?
    • The first assumption I’m making is that you have access to cron jobs through your own web server or your web host’s control panel.
    • The syntax for a cron job is “minute hour dom month dow user command”
      • minute – This is a numeric value that can be set between 0 and 59. The cron will run at this minute of the hour.
      • hour – This is a numeric value that can be set between 0 and 23 where 0 represents midnight. The cron will run at this hour of the day.
      • day of month (dom) – This is a numeric value that can be set between 1 and 31. The max value is dependent on the month you will select next.
      • month – This is a numerical value that can be set between 0 and 12.
      • day of week (dow) – This is a numerical value that can be set between 0 and 6.
      • user – This is the user that will execute the command.
      • command – This is the actual unix command that will be executed by the user specified at the specific day and time selected.
    • A * may be used for any of the day or time values if you do not wish to specify one.

  • How can we use the power of a cron job?
    • Cron accepts some special values for times and day/date values in order to meet the needs of as many users as possible:
      • Lists – Lists are accepted in a comma-separated list set.
        • Ex: dow = 1,3,5. This would schedule the cron job to run on Monday, Wednesday, and Friday.
        • Ex: dow = 1-5. This would schedule the cron job to run Monday through Friday.
      • Steps – Steps are accepted in a division style format.
        • Ex: hours = */4. This would schedule the cron job to run every four hours.
      • Lists and Steps can be combined to arrive at the desired schedule.

  • Examples:
    • 30 2 * 6 0 startrak118 php -q /home/www/site_backup.php – This cron would execute the site_backup.php file from the /home/www/ directory through php every Sunday of June at 2:30 am.
    • 0 */4 * * * startrak118 php –q /home/www/site_stats.php – This cron job would execute the site_stats.php file from the /home/www/ directory through php every four hours.

Do you have any good tips or pointers to help us understand cron better? Leave us some comments so we can continue to discover the power of cron jobs.



One thought on “Discover the Power of Cron Jobs”

Comments are closed.