Executing php with crontab

I'm trying to run a php-script on a scheduled basis. So I'd thought crontab was a good idea. The server I'm using is on Linux which I'm not that familiar with. So the problem I'm having is, I don't know how make the script executable from php. I need to reference the script, or put it into a folder that can run php from the command line. So I don't know what path to give my crontab, for example:

5  * * * * var/www/some/path/script.php

I found some vague information about this php executable being found in

/usr/bin/php

But I can't find any php file in there, maybe I don't have it installed? My php5 and apache installation is in:

/etc/php5

So my question becomes, is there anyway to execute a php-script with crontab in any other folder, or do I just lack the php executable in usr/bin/php?


Solution 1:

Start by typing at a command line:

whereis php

Do this as the user that the cron job will be run under. This will show you the path to your executable. You can then use that path (if it's not already in your PATH variable) in your cron entry:

5 * * * * /your/path/to/php /var/www/some/path/script.php

Edit: you may need to install the php5-cli (Ubuntu package name) package if all you have is the Apache PHP module installed. This will give you the binary executable that you can run from a command line.

Solution 2:

Is this a Linux system?

In newer Linux distributions, there is
actually a convienient crontab-setup system
that doesn't require any entry in the crontab by the user. E.g in SuSE Linux, you have directories

/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.monthly/
/etc/cron.weekly/

Just put an invocation script (konno_php_start) in any of these directories, like

/etc/cron.hourly/konno_php_start

which is executable (chmod 755 or so) and contains:

#!/bin/sh
cd /var/www/some/path/
php  script.php >> logfile.txt 2>&1

and restart the cron daemon. Thats it.

From the logfile, you'll see if your php interpreter
will be found in the PATH. If not, change the line in /etc/cron.hourly/konno_php_start to

/full/path/to/php  script.php >> logfile.txt 2>&1

Regards

rbo

Solution 3:

You can use the wget command locally:

5  * * * * wget http://localhost/some/path/script.php