Page 1 of 2

triggering php scripts using cron

Posted: Tue Mar 29, 2005 10:50 am
by dannymc1983
im currently setting up a script wherby emails to be sent out from a website are stored in a database. then using a cron program the database is checked for new emails every 15 mins and these emails are then sent. The only problem i have is that to run a script i have to run it in a browser under the address "http://localhost/'the name of my script .php'"
this would mean that to execute the script i would have to enter the location of the web browser with the above address as a parameter in the cron file.
this would execute the script but it means a new browser is opened every 15 mins which i can see, which after a while results in a lot of open browsers!
is there anyway i can run a php script without having to run it in a web browser or is there a way that the script can just simply run in the background without it being visible to the user?? any help would be appreciated thanks!

Posted: Tue Mar 29, 2005 11:14 am
by feyd
I'm guessing you're running this "cron" under Windows.. in which case, you can run the script through the command line interface to php, which will open a DOS emulation window briefly, if I remember correctly. If these are run under a UNIX machine, then simply adding a shebang (#!) line as the first line in the file, will allow the OS to call your command line interface for you by simply calling the script.

Posted: Tue Mar 29, 2005 11:21 am
by dannymc1983
Yes im running windows, but as i said in my first post the only way i can run a php script is in a web browser under my server:
The only problem i have is that to run a script i have to run it in a browser under the address "http://localhost/'the name of my script .php
say for example if i have a .php file on my desktop and i try to open it then none of the php code in that file will be executed in it (and i dont know why).
do you know of a way i can run my php scirpts without having to open them in a browser?

Posted: Tue Mar 29, 2005 11:24 am
by Chris Corbyn
In command line:

Code: Select all

# C:\>  c:\php\php.exe c:\webs_dir\phpfile.php
You can run it as cron by putting the command into Scheduled Tasks under the Windows Control Panel :wink:

Posted: Tue Mar 29, 2005 11:47 am
by dannymc1983
ok thanks for that. tried running the script in the command line but its giving me a fatal error thats not appearing when i run it in the web browser! it works fine in the browser.
it doesnt seem to recognise a method thats being called (the method is part of another file). maybe this could be because the includes are treated differently when running under the command line?
the script im trying to run by the way is a pear script, mail_queue, for storing emails in a database and then sending them when a cron task executes

Posted: Tue Mar 29, 2005 3:01 pm
by infolock
try turning error reporting on and i'd say you'll see the errors in your web browser as well. but to any extent, no matter where you execute the script (browser or console) it's still getting processed by php engine.

could you post the error code? maybe that will shed some light on the subject...

Posted: Tue Mar 29, 2005 4:06 pm
by Chris Corbyn
You may need to specify the location of php.ini if it requires settings made in there.

Use the -c (lowercase "c") flag to do this.

Code: Select all

# C:\> c:\php\php.exe -c c:\windows\php.ini -f c:\webs_dir\phpfile.php
EDIT | Reason: PHP running in CLI doesn't load php.ini, it just uses the defaults.

Posted: Tue Mar 29, 2005 6:30 pm
by dannymc1983
thanks d11, got it working with your suggestion. the windows ini file didnt work for some reason but i tried the ini file in the apache folder and that one did the trick. much appreciated!

Posted: Tue Mar 29, 2005 6:48 pm
by infolock
there is also a way to configure windows so that when you run a php script (either by console or double click) that it tries to execute it with php.exe. i'd have to do some searching on the forums, but i posted on this method somewhere...


EDIT : Found it. You can go here and follow the links supplied.

Posted: Tue Mar 29, 2005 9:03 pm
by dannymc1983
ok thanks, i already have a cron program (nncron lite) which is basic enough but it does the job.
do you know of any way of setting tasks so that they run in the backgound, so for example if i didnt want the dos prompt window to appear while my cron task is executing?? there must some option you can set on the command line which will take care of this. would you know anything about this?

Posted: Tue Mar 29, 2005 9:36 pm
by feyd
Last I heard/read, that'd require a recompile with some code changes to make it a Win32 app.

Posted: Tue Mar 29, 2005 11:45 pm
by Pyrite
I dunno if this applies to php4, but in 5, php-cgi.exe -q file.php doesn't show the dos window.

Posted: Wed Mar 30, 2005 12:01 am
by dannymc1983
regarding your post feyd, are you sure something that drastic would be required???
surely there must be some dos option that can be applied regardless of the file type that you are trying to execute?

Posted: Wed Mar 30, 2005 12:15 am
by feyd
there is no dos option what-so-ever. It's all based on how the exe is written/linked.

php-cgi is different from the php command line interface.

Posted: Wed Mar 30, 2005 12:33 am
by dannymc1983
ok, i tried using the following command:

Code: Select all

c:\wamp\php\php-cgi.exe -c c:\wamp\apache\php.ini -q c:\wamp\www\send_example.php
and the dos window is still appearing (it is not running in background mode). any other suggestions?