Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

CGI and System

by perlknight (Pilgrim)
on Feb 18, 2005 at 17:54 UTC ( [id://432443]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, I have a system command which needs to be run browser. I am using Perl CGI to build the web interface. However, I am running
system("<some command which takes long to complete> &");
When using it via web, it works but, the web interface seems to waiting for the command to complete. Is there a way to shoot off a long running command w/out having the browser waiting/apache to timeout on it.

Just curious, would exec work, since it does not bother to return the status of it. Like, what's in $?. Will try it out once I get home.

Replies are listed 'Best First'.
Re: CGI and System
by eclark (Scribe) on Feb 18, 2005 at 22:01 UTC

    Wrap your head around this. It forks away from the cgi, detaches the child process and execs your command. If you're just running a perl script, you can replace the system call with a function you want to run in the background.

    use strict; use POSIX 'setsid'; my $command = q{perl -e 'sleep 30'}; if (my $pid = fork) { exit; } elsif (defined $pid) { open STDIN, '/dev/null'; open STDOUT, '>/dev/null'; open STDERR, '>/dev/null'; setsid(); unless (exec $command) { die "Couldn't run process!"; } } else { die "Cannot fork: $!"; }
Re: CGI and System
by Tanktalus (Canon) on Feb 18, 2005 at 18:05 UTC

    You want to close stdout in the subprocess. Try

    system("blah blah blah > /dev/null &");
    There are many other ways to do this, too - I'd probably perform my own fork, and in the child I'd close STDOUT, and then exec(qw(blah blah blah)), but I prefer the list version of exec and system over the scalar version.

      sorry, I have tried this as well.
      system("bla blah > /path/blafile &");
      this has the same result.
        Might I ask what it is you are doing? You might want to examine the command to see if there's a way to spead it up.

        Also, remember that I/O to a device (in this case, a file on a drive) is often the slowest part of a system. I believe that writing to /dev/null rather than an actual file can speed things up if you don't need to store the output. At least, I think it does, or at least did once upon a time. If you can redirect to /dev/null instead, that might help.

        --
        tbone1, YAPS (Yet Another Perl Schlub)
        And remember, if he succeeds, so what.
        - Chick McGee

        Last "try this - I haven't" idea: close stderr, too. Perhaps 2> /dev/null would be about right. Ideally, you'd fork, close stdout and stderr in the child, then exec your command, still in the child. Or, in your case, redirect stdout to a file, close stderr, then exec your command.

Re: CGI and System
by ww (Archbishop) on Feb 18, 2005 at 18:04 UTC
    searching "browser timeout apache" might be educational, for starters.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://432443]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found