Posted on

Cron jobs and crontab

What is cron and what is crontab

Cron is a time-based job scheduler on Linux systems used to execute jobs based on a cron table (crontab). Some information can be found at Wikipedia:
Cron on Wikipedia

How to list existing cronjobs?

Get a list of the existing crontab by using crontab -l or crontab -l -u user in the case you want to see it for a different user.

The crontab below has the documentation for the entries and one sample entry to execute the script myscript.cron every 10 minutes.

$ crontab -l

# *    *    *    *    *  command to execute
# -    -    -    -    -
# |    |    |    |    |
# |    |    |    |    |
# |    |    |    |    +----- day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names)
# |    |    |    +---------- month (1 - 12)
# |    |    +--------------- day of month (1 - 31)
# |    +-------------------- hour (0 - 23)
# +------------------------- min (0 - 59)

# every ten minutes
*/10 * * * * /etc/cron.hourly/myscript.cron

# every day at 13:00 and 19:00
00 13,19 * * * /etc/cron.hourly/myscript.cron

How to add an entry to the crontab?

$ crontab -e

How to execute a php script with crontab?

This is an example of the myscript.cron from above which executes the .php script.

$ less myscript.cron

#!/usr/bin/php
<?php
include "/var/www/html/whatever/cron/updates.php"
?>