PHP with Cron


Hi

I'm trying to create a cron job to run a script at midnight every night, however I've not been able to successfully get it to run.

So I tried running the script in shell by doing the following:

/usr/bin/php /usr/local/home/httpd/docs/affcheck/dcm/grab.php

The output is...

X-powered-By: PHP/4.0.6
Content-type: text/html

<br>
<b>Fatel error</b>: Failed  opening  required 'TimeZone.php' (include_path='.:/opt/php-4.0.6/lib/php') in <b>/usr/local/home/httpd/docs/affcheck/dcm/Classes/Date/Date.php</b> on line <b>25</b><br>


The script TimeZone.php is in the same dir as the Date.php script and is called like this:
require_once 'TimeZone.php';

The Date.php script is called from the grab.php (i.e the script that is cron'd) like this:
include "./Classes/Date/Date.php";

Also the script runs fine in the browser with no errors.

* I checked /usr/bin/php was the correct dir by running which php in shell

Any help would be great.

Also this is my first cron job and I'm not great with *nix either, so simple is good :-)
dealclickcoukAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
gnudiffConnect With a Mentor Commented:
1. Yes, you should put the whole line, and you don't need the /dev/null in the end.

You can't redirect output via > or >> because wget fetches the file not to stdout, but to the file (in this case 'grab.php'), by default. The only things that can be redirected is wget info about how it fared, but that is moved to quiet mode via the -q option already.

So, if you omit -O /dev/null, you would end up with a grab.php file in your directory. In addition, wget does not overwrite existing files, so you'd have files like: grab.php, grab.php.1, grab.php.2 ... one more after each run.

2. you do not add user to cron entry, because cron jobs are separate for each user.
if you are a root, you can edit other users' cron jobs, but else everybody is restricted to their own.

see "man crontab" for cron entry format (scroll down a bit), here is a quote from there:

# all the entries run the comand "date":

# MIN HOUR DAY MONTH DAYOFWEEK   COMMAND
# at 6:10 a.m. every day
10 6 * * * date

# every two hours at the top of the hour
0 */2 * * * date

# every two hours from 11p.m. to 7a.m., and at 8a.m.
0 23-7/2,8 * * * date

# at 11:00 a.m. on the 4th and on every mon, tue, wed
0 11 4 * mon-wed date
0
 
gnudiffCommented:
Yes, invoking the same scripts from browser and from shell can be different, as the PHP environment changes:

1) you are probably calling script from cron as "yourself" not as the web-server user, who usually runs all the scripts;
2) php called from command-line usually does not know about the php settings that are available to the php Apache web module.

The quickest solution might be to simply request that page via web from command line.
You would need a "wget" or similar utility (comes as standard on most Linux distributions).

then you can use something like:

wget -q -O /dev/null 'http://localhost/affcheck/dcm/grab.php'

in your cron job entry; that should execute the grab.php page, do not show any messages, and drop the page content to /dev/null (ie discard it). the URL should be of course the same as you use normally.
0
 
chippleCommented:
Try putting the whole path in your include.

include('/usr/local/home/httpd/docs/affcheck/dcm/TimeZone.php');

Good luck!
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
dealclickcoukAuthor Commented:

Thats great, I ran

wget -q -O /dev/null 'http://localhost/affcheck/dcm/grab.php'

and it worked!

Just to confirm, do I put the whole line into the cron job, or should I omit the "-q -O /dev/null" bit and then add >> /dev/null to the cron command i.e.

00 00 * * * wget 'http://localhost/affcheck/dcm/grab.php' >> /dev/null

or should it be

00 00 * * * wget -q -O /dev/null 'http://localhost/affcheck/dcm/grab.php' >> /dev/null

also do I need to add a user to the cron line...e.g.

00 00 * * * root wget 'http://localhost/affcheck/dcm/grab.php' >> /dev/null

Thanks again


0
 
dealclickcoukAuthor Commented:

OK

I now have the following in my cron job:

10 15 * * * wget -q -O /dev/null 'http://localhost/affcheck/dcm/grab.php'

i.e. it was about 15:00 when I set it up to test, but nothing seemed to happen, if I change the /dev/null to a log file so I can check for any errors, where will it be put?

Thanks again
0
 
gnudiffCommented:
Change it to:

MAILTO=root
10 15 * * * wget  'http://localhost/affcheck/dcm/grab.php'

if it works, it should put the grab.php file in that user's homedir, whose cron it is, also the output from the wget command would go to root's email.

Also, check if your server's time is set right ("date" command) -- it might've been after 15:10 in your server even if your watch showed 15:00 ;)
0
 
dealclickcoukAuthor Commented:
sorry to be dumb, but where do I put the MAILTO=root is it a seprate line in the cron file
0
 
gnudiffCommented:
yes, it can be the first line of the cron file - this is a format how you can set various cron operational parameters.

it may be that you don't need it, but if you are not receiving any mails from cron, add it (of course, you can select any other account on your machine, instead of root).
0
 
dealclickcoukAuthor Commented:
problem was to do with the rc.conf file, as by default my server providors set this to NO...

# Set to YES to enable the cron daemon
cron_enable="YES"

but when setting up cron jobs via crontab it says installed, and gives no suggestion that it's not installed.

Anyway just adding this for future users reading this, thanks for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.