Solved

PHP with Cron

Posted on 2004-09-03
9
350 Views
Last Modified: 2012-05-05

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 :-)
0
Comment
Question by:dealclickcouk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 3

Expert Comment

by:gnudiff
ID: 11970953
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
 
LVL 4

Expert Comment

by:chipple
ID: 11970965
Try putting the whole path in your include.

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

Good luck!
0
 

Author Comment

by:dealclickcouk
ID: 11971466

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Accepted Solution

by:
gnudiff earned 50 total points
ID: 11972679
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
 

Author Comment

by:dealclickcouk
ID: 11973065

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
 
LVL 3

Expert Comment

by:gnudiff
ID: 11975426
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
 

Author Comment

by:dealclickcouk
ID: 11976816
sorry to be dumb, but where do I put the MAILTO=root is it a seprate line in the cron file
0
 
LVL 3

Expert Comment

by:gnudiff
ID: 11995579
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
 

Author Comment

by:dealclickcouk
ID: 11995656
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

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question