Google Answers Logo
View Question
 
Q: Trouble setting a cron job on FreeBSD 4.4 to run a PHP script ( No Answer,   2 Comments )
Question  
Subject: Trouble setting a cron job on FreeBSD 4.4 to run a PHP script
Category: Computers > Internet
Asked by: barryfreed-ga
List Price: $15.00
Posted: 26 Jan 2004 07:06 PST
Expires: 25 Feb 2004 07:06 PST
Question ID: 300278
Hi.
I am having a little trouble with setting up a cron job. Here's the info:

What I'm trying to do:
Using PHP and a MySQL database, I am going to grab information from a
news database with PHP. I want cron to run the php script and then
copy the dumped file to a directory and rename it as an xml file. I
have a news database in MySQL that stores news shorts. The PHP script
will grab the headline, description and link. The script will then
format the output as a valid rss feed. The only problem with this is
that the file will still be .php. This is why I want to run the cron
job.
So, once the script is executed and in valid rss (xml) format, I want
cron to grab the html output, copy it to the proper directory, and
rename it feed.xml.
Now here's the problem. 
I have been looking around online and have seen that accomplishing
this task should be very easy. The script is a piece of cake (and I'll
be able to do that in about five minutes), but the cron job is simply
not working. I am using lynx to run the php script. If I am in a
telnet session and run the following command:

lynx -dump http://www.domain.ext/script.php>/www/vhosts/domain.ext/htdocs/feed.xml

everything works perfectly. Lynx runs the script, and cron writes the
file. all is well. But when I try to do it in cron, cron will look
like it's running the script, and will simply create a 0 kb file in
the directory called feed.xml with nothing in it. Note: cron will only
do this when there is no file there already named feed.xml. It won't
overwrite.

Important Info:
Server OS: FreeBSD 4.4 

Now, the crontab is set up completely differently than any other os
I've used in the past. Rather than having a cron folder with
cron.daily, weekly, monthly etc., there is just a crontab that points
to other scripts. So here is the crontab, with the last entry the dump
that I'm trying to do.


####################################################################

# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD: src/etc/crontab,v 1.21.2.3 2000/12/08 10:56:07 obrien Exp $
#
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
HOME=/var/log
#
#minute hour    mday    month   wday    who     command
#
*/5     *       *       *       *       root    /usr/libexec/atrun
#
# rotate log files every hour, if necessary
0       *       *       *       *       root    newsyslog
#
# do daily/weekly/monthly maintenance
1       3       *       *       *       root    periodic daily
15      4       *       *       6       root    periodic weekly
30      5       1       *       *       root    periodic monthly

# time zone change adjustment for wall cmos clock,
# does nothing, if you have UTC cmos clock.
# See adjkerntz(8) for details.
1,31    0-5     *       *       *       root    adjkerntz -a

# Clean out the popauth database at 3am
2       3       *       *       *       root    /usr/sbin/vcleandb

#test 2
*3      *       *       *       *       root    /usr/sbin/dump

####################################################################

So, this is how I have the /usr/sbin/dump file set up:


#!/bin/sh
/usr/local/bin/lynx --dump
http://www.domain.ext/script.php>/www/vhosts/domain.ext/htdocs/feed.xml


####################################################################

So to wrap up, the script can run using the above command, but won't
do it in a cron job. Please let me know how I can get the script to
run and how to get it to overwrite when the file exists. I want the
script to run every hour or so, and to overwrite the existing feed
file. I've been working on this for a day now, and I have tweaked so
many different things. If you have any questions, please let me know.

Thanks
Nathan

Request for Question Clarification by weaver-ga on 29 Jan 2004 15:20 PST
I don't have an absolute answer here, but let's run through a few
things and maybe the answer will strike us.  You're right in assuming
this should be a pretty simple task to get finished.

First, the only reason other Unixes can do the cron.daily,
cron.weekly, etc. is because they have an entry in their /etc/crontab
file which runs a program called run-parts on the specified directory.
 You could do the same on FreeBSD.  The run-parts program might not be
installed by default though.

# run-parts
* * * * * root run-parts /etc/cron.minute
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

I don't see anything wrong with the syntax of your lynx command.  I
tried it out with a different domain here and it worked fine.  You
should be careful naming your script dump.  Most Unixes come with a
program called dump that would be in /sbin/dump.  But, since you
specify the complete path name, you should be fine there.

I think you have a typo in your crontab file, but I don't think it
should prevent this from working.  Your test line starts with *3.  My
crontab program ignores the 3 and runs the script every minute.  Did
you  mean to have */3?  Which would run the script every three
minutes?

Make sure /usr/sbin/dump is executable (just covering all the bases). 
Make sure you've got the path to lynx correct in the script.  If not,
my system creates a 0 byte file just as you are describing.  Use
'which lynx' to find the correct path.

This setup should work, there must be some small detail that is being
overlooked on your system.  Hopefully this will help you find that
small detail.  Let me know if my comments were of any help and I'll
repost this as an answer.  If not, let me know and perhaps I can think
of more things to try.

Clarification of Question by barryfreed-ga on 30 Jan 2004 11:09 PST
Hi.
Thanks for taking a stab at this head-scratcher.
I did a 'which lynx' and sure enough, the path the lynx is exactly
correct. Also, the /usr/sbin/dump file is executable. I've also taken
your advice and renamed it dumb rather than dump, and it still won't
work.

Any other ideas?

-thanks again,

Request for Question Clarification by weaver-ga on 30 Jan 2004 11:51 PST
Another thing to try.  You can run most PHP scripts from the command
line.  You can either run 'php script.php > /path/to/output.xml' or
you can make a copy of the script and change the first line to
#!/usr/bin/php (or wherever your php binary is) and then just run
'script.php > /path/to/output.xml'.  Then you can either call the PHP
script from your /usr/sbin/dumb script or run it directly from
crontab.

I still am not seeing why this is a problem, but perhaps taking lynx
out of the loop will help.

Weaver
Answer  
There is no answer at this time.

Comments  
Subject: Re: Trouble setting a cron job on FreeBSD 4.4 to run a PHP script
From: quinto-ga on 24 Feb 2004 09:06 PST
 
Your /etc/crontab file uses the option "--dump" for lynx.  From the
lynx manpage, it appears that this should be "-dump" instead.
Subject: Re: Trouble setting a cron job on FreeBSD 4.4 to run a PHP script
From: cbuechler-ga on 08 May 2004 20:48 PDT
 
Just a stab in the dark, but I've had problems with shell scripts
running from cron before as well.  Generally when a shell script runs
fine at the command prompt but doesn't run from cron is because of
some environment difference(s).

Try changing #!/bin/sh to #!/usr/local/bin/bash or the full path of
whatever shell your user account uses (doubt it's /bin/sh).  That
alone has worked a couple times for me.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy