tags:

views:

452

answers:

5

Hi everyone. 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?

+5  A: 

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.

richsage
You can also use 'which php' in the command line
Manos Dilaverakis
whereis php gave me the answer "php :" Not very informative. I might have to consider installing that package then. I read something about running the crontab via Lynx for some reason if your php was installed in Apache mode and not CGI. My phpinfo(); said Server Api: Apache 2.0 Handler
Stefan Konno
Getting back "php: " on its own means that the executable couldn't be found - so you may need to install the package to get it.
richsage
Guess I'll go google it for a while on how to install it. I'll get back to you if that's ok? If I can't find anything useful that is. Thanks for the help, I really appreciate it.
Stefan Konno
No problems. Depending on your OS, you could use "apt-get install php5-cli" or similar but probably best to check your individual OS's package manager for details.
richsage
apt-get install php5-cli just gave me a bunch of errors, seems it can's find the .deb file it's suppose to find.
Stefan Konno
I just wanted to let you know I fixed it. I first had to run apt-get update in order to update the list from ubuntu repository. Then run apt-get php5-cli and then whereis php gave me the right path. Thanks alot for your help, you really saved my day!
Stefan Konno
Pleasure :-) glad you got it working!
richsage
A: 

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

rubber boots
How do I restart the cron daemon?
Stefan Konno
SuSE: (as root) "rccron restart" Slackware-like: /etc/init.d/cron restart others: (google)
rubber boots
start-stop-daemon: warning: failed to kill 5097: Operation not permitted...fail!is what I got, but I'm not currently logged in as root. Do I need to be root to edit the crontab as well. Sorry if I'm asking stupid questions, I'm a bit new to the whole server thing.
Stefan Konno
Stefan, in order to handle system daemons, you need to be root. BTW. What Linux Version or Distribution is this?
rubber boots
You don't really need to restart cron daemon.
greg
"Just put an invocation script (konno_php_start) in any of these directories" - No - these directories are for system maintenance scripts not application stuff. And you don't know WHEN in the hour/day/month/week the script will run. And the OP wanted to run his script every 5 minutes.
symcbean
A: 

You can also use env, it will find and launch php for you:

/usr/bin/env php /var/www/some/path/script.php

Or you can place a shebang in your script.php (first line):

#!/usr/bin/env php

then make it executable, and make crontab call it directly, like in your first example:

5  * * * * /var/www/some/path/script.php
Victor Stanciu
Do I write this in the crontab?
Stefan Konno
Yes, instead of your initial command.
Victor Stanciu
A: 

I suggest that you do like this,

*/5 * * * * /path/gridmon2.pl 1> /dev/null 2> /dev/null

where in you .pl code you should grep using wget or something like this:

wget "/www/root/index.php"

or you can just do like this:

/usr/bin/wget "/www/root/index.php"

It's just my suggestion, I've only try wget to external site not locally and it works.

please try and revert.

Nazmin
you can also using as .sh file, same things...
Nazmin
A: 

Using cron for a thing such as this is not a great idea IMO.

Why don't you just modify the php script to run as a daemon?

Or build a daemon yourself, that will execute the program?

It's very easy in php.

added example:

daemon.php:

while (1) {
    shell_exec("php subdaemon.php");
    sleep(60*5);
}

subdaemon.php:

$interval=3600*24;
$last_run=get_last_run();
$time=time();

if ($last_run+$interval>$time) die();
do_stuff();

save_last_run($time);
Lo'oris
Cron is exactly the right tool for the job. Running PHP as a daemon is feasible BUT NOT EASY; the standard memory management model means you really need to know what you are doing to create a stable system.
symcbean
what do you mean "not easy"? it's TRIVIAL! (see added example) understanding cron mad syntax is much more difficult than actually writing the daemon yourself, lol.
Lo'oris
@Lo'oris cron job management tools are available from many hosting services, even in some with no SSH access. **PHP daemons instead are subject to bugs that could kill them**, so they still would need some kind of watchdog to alert admins or put them back online if they die unexpectedly.
ZJR
if a 4-line daemon like the one above crashes, I'd say there is something very wrong at a lower level
Lo'oris