Forum Moderators: coopster & phranque

Message Too Old, No Replies

CRON environment

$env{document_root}, $env{server_name}, $env{server_admin}

         

WWMike

4:34 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



I have a PERL cgi script that run flawlessly from the browser but when run as a CRON it apparently doesn't have the $ENV{} variables populated.

I am specifically needing:

$ENV{DOCUMENT_ROOT}
$ENV{SERVER_NAME}
$ENV{SERVER_ADMIN}
and the current script directory which is usually the default if no other prefix is provided when accessing a filename.

How does one avoid hard-coding all those entries that $ENV{} makes available?

ChadSEO

6:11 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



Those variables are generated by the web server, so they would be unavailable if you are running the script locally. One solution would be to call the script from a website through your cronjob, by using wget or a similar tool. If wget is installed, you could simply do

* * * * * wget [mydomain.com...] > /dev/null 2>&1

WWMike

7:40 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



Many thanks for that. It sounds like the right solution but I'm still waiting to hear if wget is installed. On a similar note, if I wanted the output appended to a log.txt file in the same folder as the script and email notification sent to $ENV{SERVER_ADMIN} what would the last 2 parameters actually be?

ChadSEO

7:50 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



To append the output to a file would be:

* * * * * wget [mydomain.com...] >> /path/to/log.txt 2>&1

If you wanted to send an email to $ENV{SERVER_ADMIN}, this would have to be done within the perl script, because it has access to those environmental variables.

WWMike

10:03 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



OK, the wget apparently worked so many thanks for that.

However, I'm also getting a bunch of wget messages and menu text in the log in addition to just the expected "Done" from my script and according to [weather.ou.edu...] there should also be a cron option for automatic email notification but the one time that I even came close to getting the cron to run previously I got "No recipient addresses found in header".

Any ideas on how to suppress the wget messages and also, what header?

ChadSEO

10:14 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



To get the output of just the page, and not the wget junk, try

wget [mydomain.com...] -O /path/to/log.txt > /dev/null 2>&1

As far as cron automatically emailing, if the cron job generates any output, it will email the results to the user on the current server. So if your webserver is also your mail server, or it is setup to forward email to your mail server, and the username is the same as your email, then you should receive a message. You could also add an extra command to send an email to any address, something like:

* * * * * wget [mydomain.com...] -O /path/to/log.txt > /dev/null 2>&1; cat /path/to/log.txt ¦ mail -s "cron notification" WWMike@mikesemail.com

WWMike

10:26 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



OK, I'll try adding the -O switch and if my host can't get the email notification to work thru config I'll add the notification manually as you suggested or just include it in the script but it's all downhill from here - that wget was the key. Many thanks!

WWMike

8:40 pm on Jul 23, 2005 (gmt 0)

10+ Year Member



OK, I'm almost there but not quite...

I dabble in PERL but I drown in UNIX and I don't have access to a UNIX prompt so for testing purposes, I brute-forced a quickie PERL script to simulate the bulk of the CRON command <wink>:

#!/usr/local/bin/perl

system("/usr/local/bin/wget -q -O - [mydomain.com...] >> /usr/home/myaccount/scripts/mydomain.com/script/log.txt");

print "Content-type: text/html\n\n";

exit;

OK, here's a quick summary for review:

I need to use wget to run my script because my script needs the PERL $ENV{} variables but UNIX CRON doesn't provide them.

The -q tells wget to be quiet and supress its (useless) messages while the -O - tells wget to send the script output to STDOUT instead of writing it to a file (which is the wget default).

I should also mention that according to the wget docs at [gnu.org...] there doesn't seem to be an option to have wget append the script output so I have to resort to redirecting it with the UNIX >>.

2 questions:

1) Why doesn't the UNIX >> append to the log file?

It creates it if it doesn't exist but it always overwrites it after that and I can't seem to get a running log.

And also...

2) Why do I have to include the print to eliminate the 500 error I get without it?

WWMike

10:55 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



Well, I thought wget would do the trick but I had to abandon using it because it seems to be causing erratic behavior by somehow running my job several times in rapid succession.

My host swears that the system logs prove that CRON ran only once per day as should be the case but somehow my script ran 6 times within 3 minutes today and 2 times within 1 minute a couple of days ago.

There is absolutely no way that my script could be looping or running itself more than once and if I trust my host to be correct then that leaves wget as the sole culprit.

Anyone have any previous experience with this type of issue?

KevinADC

7:01 am on Aug 4, 2005 (gmt 0)

10+ Year Member



I was thinking maybe someone clicked your link:

[mydomain.com...]

but on closer inspection that looks like a dummy URL.

kerrycolligan

2:23 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



The 500 error is related to your system() hack. Do a perldoc system and you'll see:

The return value is the exit status of the program as returned by the wait() call. To get the actual exit value divide by 256. See also exec. This is NOT what you want to use to capture the output from a command, for that you should use merely backticks or qx//, as described in `STRING`.

If you omit the print statement after the system call, there is no output. The 500 error results from making a CGI web request that produces no output.

kerrycolligan

2:28 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Also, take another look at wget --help.

-O --output-document=FILE write documents to FILE.
-o, --output-file=FILE log messages to FILE.