php/cron question..


#1

hi all,

if i set a php script to run via cron, and in that php script i include another script…does the included script get executed too?

Thanks
Colin :)


#2

If you inlude() a script, it should run as long as it’s syntactically correct.

On that note, however, I don’t know if you can run a PHP script via cron.

For cron jobs you are better off using a shell script.

Good luck


#3

yes, it does

one way to run a php script via cron is if you have lynx and your script can be accessed via the web, to do this enter this in your crontab…

lynx -dump http://mysite.com/script.php

That will work just as if you opened it in your web browser


#4

I think my response should been more of WHY you would want to run a php script as a cronjob.

You are displaying an HTML page (via php) for no one to see. Moreover, it seems like a waste of resources. You access Lynx to display a page (that noone sees), you access PHP which is accessed thorugh the webserver (apache?). The only real reason I can think of to run a PHP script that no one needs to see the output (directly) is perhaps to do some updating of files or maybe a database. This means someother resources like mySql being used. In either case a typical bash shell script (or perl or some other shell scripting) would seem to be more appropriate to that task.

Just my 2 cents…


#5

Actually, you don’t even need to use lynx to do so. You can simply call the php interpreter directly in the cron (unless remote, but even there, you could do it using ssh).

/usr/local/bin/php /home/joe/script.php

It will actually have the same permissions as the user running it. The nice part is that you don’t have to give those permissions to your web server.

The other solution is to create an executable directly:

#!/usr/local/bin/php
<?php
echo "Hello World!n";
?>

Just make sure the script has execute permission.

PHP works jsut fine for shell scripts. One of the huge advantage is that you will be able to use the libraries you coded for your website. It also has a much better syntax than bash anyway.


#6

Well, I guess I learned something new today.

I certainly can see the advantages of using php, but I was unaware that you could run it like that.

Knowing that may help me with other tasks.

Thanks Bane (I guess you aren’t the “All Knowing” for nothing.)


#7

That’s only the result of reading way too much documentation and experimenting over 5 years, but I don’t know everything yet, I still have to learn a lot.


#8

Well thanks for the replies :smiley: