Linux Cron Job Tutorial: running a .php file

Comments 3 Standard

All of my database driven games use PHP as a scripting language which is my favorite scripting language by far (okay I’m biased, I’ve been using it for almost 9 years now).

In order to propagate changes each day I setup a cron job, or a scheduled script that will run a certain times of the day. In a nutshell this updates all of the underlying game structure so pets get older, shows automatically run, so forth and so on.

Accessing the Cron

Although you can edit the cron file directly (var/spool/cron/crontabs)  it’s easier to use the crontab command and let it do most of the work for you. Open a shell and type the following command. If you’re not a root user you’ll need to use sudo before these commands (and have super user permissions).

crontab -e

Editing The Cron

The syntax for the cron jobs you’ll be adding to this file is:

minute hour day month dayoftheweek /path/to/script

Minute is 0 – 59

Hour is on a 24 hour clock (0 – 23). With military time you subtract 12 from the number to get pm values. For instance take 15 and subtract 12 and you get 3pm. Everything earlier than 12 is am. The only exception are 0 and 12. 0 is midnight and 12 is noon.

Day is 1 – 31

Month is 1 – 12

Day of the week is 0 – 7 where Sunday is 0 or 7

Now to the good part, some examples. Let’s run a file at exactly midnight every day:

0 0 * * * /path/to/file

The asterisks indicate that this script will run every day. If we want to run the file at 12:15 midnight:

15 0 * * * /path/to/file

Let’s run a script on the 30th of the month at 2:45pm:

45 14 30 * * /path/to/file

Now I only want to run this script on October 31st every month every 25 minutes:

25 * 31 10  * /path/to/file

Last but not least I want to run this file every Tuesday at Midnight in March every 10 minutes:

10 * * 3 2 /path/to/file

My only word of caution is running scripts every x number of minutes every single day in small intervals (ie * * * * *  or 5 * * * *). This will eat up your CPU and end up locking out all of your other processes.

Emailing Cron Results

By default the cron is setup to email itself to the server every night. You can change which email each cron goes to by putting the following on the line before the cron:

0 14 30 * * /path/to/file/

Turning Off Cron Emailing

Sometimes having an email sent to you for some mundane script just wastes space in your inbox. That’s okay, you can turn the email feature off also:

5 20 * * * /path/to/script >/dev/null 2>&1

Setting Up A .php Cron File Through the PHP Interpreter

Replace the path to your php interpreter shown in blue below. Follow that by the -q option and the location of the .php file you’d like to run.

0 0 * * * /usr/local/bin/php -q /path/to/file.php

Running A .php File By Accessing It Online

If you’re accessing databases or includes in your .php file then you probably want to run the file as if you were viewing it on the internet from the domain name. You can use the curl command to do that in place of calling the php interpreter:

0 0 * * * curl /path/to/file.php


Hah, sorry I’m human and not a Linux guru: x | x


3 thoughts on “Linux Cron Job Tutorial: running a .php file

  1. You’ll need to contact your hosting place in order to get them. Sometimes they have a special way for you to run cron jobs (ie cPanel or WHM or some other type of client manager).

  2. Hi Jade,
    Great post.
    hi, Jeanette, if your hosting provider doesn’t allow setting cronjobs and you’re looking for a simpler solution, you might consider a webcron services like which will load a specific URL at a given time.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s