Solved

PHP with Cron

Posted on 2004-09-03
9
344 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
  • 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now