FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Run a php Script with a Cron Job





ninjakannon
I was trying to learn about cron jobs (for a project where I'll need to schedule tasks), so I thought I'd see if could do it on my account on server 2. I need to call a php script from the cron job.

As far as I can tell, the only way for me to see if the cron job works is to generate some output when the php file is called, so I've got a file with a number that should be incremented. But nothing happens! I tested to see whether the php file I'm attempting to call actually increments the number and it does.

Although I've removed it now, my 'query' was:
Code:
*/1 * * * * /usr/local/bin/php /home/user/domains/ninjakannon.frih.net/public_html/MyFile.php


That should have called it every minute, right? I also tried doing 0-59 to see what happened with that - till nothing.

Thanks for any help!


EDIT: By the way, is it possible to create a cron job from a php script?
Xaferrow
Check this out http://www.frihost.com/forums/vt-72534.html
ninjakannon
Awesome, thanks Xaferrow! I've got things working now. I didn't find that post when I searched.

What does the -q actually mean? I assume there are several 'modes' like this - if so, got a link I could look at?

One thing I didn't expect happened, though. The php file the cron job executes has an include_once function and an fwrite function within. I specified a file for each of these to act upon that was local to the php file. However, cron attempted to find the files in /home/user. Typing the file name as ./MyFile.php didn't work either, so I've had to use the full path. Is there any way to make the php file load files locally (with respect to its own location)?
rvec
try var_dump($argv);
I think the path is in the first element of that array, you could do something like this:
Code:
<?php
$basepath = $argv[0];
include_once($basepath.'../include/include.php')


the command line help page for php:
http://nl.php.net/manual/en/features.commandline.php
Quote:
CLI is started up in quiet mode by default, though the -q and --no-header switches are kept for compatibility so that you can use older CGI scripts.
ninjakannon
After doing var_dump($argv) as you suggested I've found that $argv[0] is actually the full file path of the php file. So I created a function to remove the filename from the end and hey presto, we have the file path!

Thanks a lot for that, it's excelent!
rvec
oh yeah forgot that part, but there already is a function for it:
http://nl3.php.net/dirname
ninjakannon
Hah, that's even better! I searched, but was thinking I should do it by chopping the end off the string. php seems to have a function for most useful things you can think of.

Thanks again, rvec!
Related topics
How to run a cron job on a server
Server uptime tracker
Running a "Hello World" php script
[PHP] Running Cron Jobs Via PHP Script
Cron job
Cron job and browser output question
php script not loading
Question: PHP scripting + cron jobs
ΏCron Job en Frihost?
Browser close run a php script.
How to get a php script working under a cron job?
Running PHP Script without PHP Server?
vBulletin Config.php script
Locking database with write access
Reply to topic    Frihost Forum Index -> Support and Web Hosting -> Web Hosting Support

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.