Here is a wierd one.

I have a php script that reads data from a URL and writes it to an xml file. Works great when run from browser.

When I run it as a cronjob it will not exceute. Any other php script I try that reads data just like this will execute fron a cronjob except this one.

Here is the script.

$tide_content=file_get_contents("http://tides.obinet.net");
$local = fopen('tides.xml', 'w');
fwrite($local, $tide_content);
fclose($local);

here is the crontab entry

  • 2 * /usr/local/bin/php /home/u4/broadcreek/html/get_tide.php > /dev/null

Run every morning at 2am

Any help.

Stan

    You're right - invoking the PHP script from a web browser shouldn't be any different than invoking it from cron. As an experiment, you might want to try changing your cron job to this:

    wget is basically a web browser. So instead of running the script through PHP alone, you are telling cron to run a web browser and hit your script so the cron job more closely simulates what you were doing on your desktop. As for why this might be necessary, I can't say. It doesn't make sense.

    -o /dev/null reports all messages to the trash
    -O /dev/null saves a copy of the returned web page in the trash

    (In other words, these flags cause your script to be invoked without creating useless junk in your home directory).

      The entry is continuing to the next line should I use a / at the end og the line

      Thanks

        Do not put a / at the end ofthe line.

        Your particular editor (probably vi) is wrapping the text so that you can see it all... but it will record the entire cron entry as a single line.

        Stretching your terminal window wider might even help you verify that it is indeed being recorded as a single line.

        But don't put the / in there. It will screw up the command.

          I'm not sure why you have a > sign in the wget syntax. It seems like you're mixing the syntax of two different kinds of commands.

          This command make sense to me:
          php filename.php > output.txt

          And this command makes sense to me:
          /usr/local/bin/wget -o /dev/null -O /dev/null http://broadcreek.web.aplus.net/weather.php

          But I don't know what this means... or if it even works:
          /usr/local/bin/wget -o http://broadcreek.web.aplus.net/weather.php > -O /dev/null

          You can't really output to an output flag? If it works for you, great. But if you're having trouble getting two cron jobs to work, I'd look at the syntax first. My crontab has about two dozen cron jobs, one per row, so I know you can run more than one.

          I guess the best thing to do is run the command on the command line so that wget works exactly the way you want it to... and then put the cron timer "0 2 *" in front to make it work inside crontab.

            Or maybe your text editor isn't keeping the commands on a single line? There is a way to run crontab and specify which text editor you want to use so that you don't have line break issues. Your web hosting company can help you with that better than I can.

              Correcting my syntax worked. I was mixed up on your previous post on how to form the entry.

              Thanks again etully.🙂

                Write a Reply...