executing php page using crontab

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
noguru
Forum Commoner
Posts: 61
Joined: Thu Jun 06, 2002 4:03 am
Location: Just north of the City Of Gold, Land of Milk and Honey

executing php page using crontab

Post by noguru »

Hi

I'm trying to execute a php page through a cron job, but nothing happens. Should I use:

Code: Select all

* * * * * /usr/bin/lynx -dump http://www.mydomain.com/scripts/test.php >test.log
or rather:

Code: Select all

* * * * * /usr/bin/php -f /usr/local/.../scripts/test.php >test.log
I'm not sure of the /usr/bin/lynx an /usr/bin/php paths (I get "the file/directory doesn't exist"). How will I know if these paths are correct?

Neither way seems to execute the php script through cron, although the php page executes fine when called from a webbrowser. Further the test.log file does get updated every minute, which means the cron job is working, but the test.log file remains empty and size 0 (it's suppose to display the echo's in the php page). I even tried to put the username of the test.php's owner in the statement but still no luck. Permissions on both test.php and test.log are 777 (for now).

Any ideas how to fix the problem?

Thanks in advance.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Try
which php
which php4
to locate the correct path to use for php shell scripts

and
which lynx
which links
which wget
to figure out what path to use for web-served php scripts. (wget is more lightwieght than lynx)

php is normally in one of /usr/bin/php, /use/bin/php4, /usr/local/bin/php, or /usr/local/bin/php4 on *nix systems and unlike perl doesn't tend to be in all of possibilities.
User avatar
noguru
Forum Commoner
Posts: 61
Joined: Thu Jun 06, 2002 4:03 am
Location: Just north of the City Of Gold, Land of Milk and Honey

Post by noguru »

thanx a lot! i found it in /usr/local/bin/php. the which function is quite useful.

another thing: how can i send parameters (querystring) to the php page from the command line (test.php?p=77&q=2)? i need to send 2 parameters, which i can off course set in the page itself, but i want to prevent unwelcome internet users from just typing the path in and messing up my database... removing execute permission (to 744) did not help at all, i could still run the page from the browser...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

If you are using a command-line php interpreter, ie /usr/local/bin/php the php file does not need to be world/web readable. cron will run the script as you, so as long as you can read it the script will work.

This means it could be safe to directly embed the parameters in the script and change the permissions to stop webusers from running it.

If your script is used both by web users and the cron backend, you could
copy the script and make a seperate cron version. Which doesn't even have to be in a web serveable location. For instance my webpages live in /var/www/competitions/ but my cron scripts are at /usr/local/compinabox/bin. Apache has no ability to read anthing in my /usr/local/compinabox/bin. If you are on a hosting service and don't have quite this degree of control over the location of files you probably still have a "home" directory where you first log it, that apache can't touch directly. Normally something like /home/<username>, make a bin directory here to stick your cron scripts.

I don't beleive you can pass query arguements to cron scripts when invoked with php as no webserver is involved to parse the query string. You should be able, however, to pass command line arguments
like
my_php_script.php argument1 argument2.

The arguements are stored in the $argv global array.
See PHP Manual for more details. (Sadly this is one of their less well written pages.)

My prefered way of doing commanline php is to use "sha-bang"s ie the first line of the script is
"#!/usr/local/bin/php"
now in cron you simply do

* * * * * /usr/local/.../scripts/test.php arguement1 argument2>test.log

The "Sha-bang" tells the computer to run the rest of the file through the php interpretter so you don't need to launch php first.
User avatar
noguru
Forum Commoner
Posts: 61
Joined: Thu Jun 06, 2002 4:03 am
Location: Just north of the City Of Gold, Land of Milk and Honey

Post by noguru »

Thanks. I'll definetely try to use a bin directory under my "home" to hold my script. Meanwhile I've removed all permissions for group and other users on the php file. It seems to be working - the browser can't request it anymore, but the cron can.

Thanks for your help.
Post Reply