PHP with Cron

Posted on 2004-09-03
Last Modified: 2012-05-05


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

<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 :-)
Question by:dealclickcouk
  • 4
  • 4

Expert Comment

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.

Expert Comment

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


Good luck!

Author Comment

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Accepted Solution

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":

# 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

Author Comment

ID: 11973065


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

Expert Comment

ID: 11975426
Change it to:

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 ;)

Author Comment

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

Expert Comment

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).

Author Comment

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

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
This code tracks birthdays 3 73
move widget title down 4 19
Shopping Cart 2 71
Eloquent ORM manual paginator defaults to simple 2 23
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

773 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