Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The below script works just fine from a command prompt but never when it’s called from a web browser(it doesn’t create the date.tmp file), any hints?

sub testfork { if (($pid = fork) == 0) { exec("date +'%Y-%m-%d %H:%M:%S' >date.tmp"); } elsif ($pid > 0) { #Parent wait; open (DA, "date.tmp"); $da=<DA>; close (DA); unlink ("date.tmp") or die "Cannot unlink date.tmp ($!)" ; print "the date returned from the child process is: $da\n"; } else { print ("Could not fork: errno is $!\n"); } }
You may see script with no sense, it’s just an exercise.
Tanx, Michelle again

2003-06-07 edit ybiC: Retitle from "A quick question"

Replies are listed 'Best First'.
Re: Runs from console but not as CGI - temp file not created
by sauoq (Abbot) on Jun 08, 2003 at 04:01 UTC

    This is yet another example of why you should always check the return from open(). Instead of

    open (DA, "date.tmp");
    change it to
    open (DA, "date.tmp") or die "Cannot open date.tmp: $!";
    and then check your error log. You'll probably find it is indeed a permissions issue just as The Mad Hatter suggested. Unless you've chdir()'d at some point, you probably don't want to write the temp file to the current directory. (In other words, don't change the permissions or ownership of the directory the script is in, write the file to separate tmp directory.)

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Runs from console but not as CGI - temp file not created
by The Mad Hatter (Priest) on Jun 08, 2003 at 03:36 UTC
    The problem is probably permissions. Does the user that runs the webserver have permissions to write to the directory your script is trying to write to? The user the webserver is running as should be able to write to /tmp/date.tmp if you don't want to fiddle with permissions.
Re: Runs from console but not as CGI - temp file not created
by dws (Chancellor) on Jun 08, 2003 at 05:25 UTC
    The below script works just fine from a command prompt but never when it’s called from a web browser.

    You don't mention what web server you're using, and that can make a big difference in one of several ways. First, different web servers are inconsistent in what working directory they launch scripts in. You'd expect that to be whatever directory the script itself is, but that's not always the case. (I recall pulling my hair out when trying to debug a script on Netscape FastTrack.)

    Further, different OS/Web Server combinations have different notions of permissions. Since your problem may be one of writing, chances are good that you're using Apache on Unix or a Unix variant (e.g., Linux, FreeBSD). Here, to write a temporary file, the directory that holds the CGI must have a permission that allows whatever user the web server is running under to create a file. This isn't a "best practice". Better is to create a subdirectory that will hold only data, giving that subdirectory a "world writable" permission, and then creating the temporary file in that directory. Assuming that writeable subdirectory is "data", changing your code to

    open(DA, ">", "data/date.tmp") or die "data/date.tmp: $!";
    should be sufficient. The three-argument form of open() makes it explict that you're trying to create a file. The "or die" part will provide you with valuable information in your server logs should things fail.

Re: Runs from console but not as CGI - temp file not created
by Anonymous Monk on Jun 08, 2003 at 06:05 UTC
Re: Runs from console but not as CGI - temp file not created
by vek (Prior) on Jun 08, 2003 at 16:42 UTC

    Others have already pointed out some good tips. Always check the return value from open calls. If you had I would suspect you would have received some form of permission denied error.

    Apache, for example, will (or should) run your CGI progs as 'non-real' users for security reasons. You'll probably find that your prog is run as user nobody or www or something similar. That user probably doesn't have permission to write anything to whatever directory you're trying to create date.tmp in.

    -- vek --