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

I built a web interface for my perl script that gathers hardware address from switches, and everything works well, except when I tried working out an area for requesting a new switch to be included and added to the database.

PHP executes the script but then waits for the script to respond, and then dies, because of the length of time it takes for the script perl script to finish.

What I need is for a perl script, csh, ksh, ect, to be called instead and fork to the perl script, or in some other way, start the perl script NOT AS A CHILD PROCESS.

Because I am not so good at forking and the like in perl, I am hoping soimeone is and can come up with a solution.

The script to be run is called snmpwdr, the script that starts it needs to quit as soon as it starts it, and return "DONE" or some other simple text.

Replies are listed 'Best First'.
Re: forking, no child waiting.
by suaveant (Parson) on Sep 20, 2001 at 20:36 UTC
    system("cmd &"); works for me....

    Update sorry, to be safest
    system("cmd > /dev/null &");
    just to make sure it isn't waiting on output from the system command

                    - Ant
                    - Some of my best work - Fish Dinner

      You know, that has worked for me in the past, but not in this case, I have tried system() exec() and fpassthru(), and now recently pcntl_fork(). This perl scripts gets angry when php leaves it alone for some stupid reason. Another thing I am thinking might work for these situations, is to have the first script write a cron job to do the second script, and then the second script will always remove itself from any cron entries, but I am hoping for a simpler solution, if Perl can fork, or whatever.
        Hrm... yeah... try putting this in the perl script you are calling...
        fork && exit(); #background process close(STDOUT); close(STDERR); close(STDIN);
        see if that detaches it... now you should not need th > /dev/null &

                        - Ant
                        - Some of my best work - Fish Dinner

Re: forking, no child waiting.
by PyroX (Pilgrim) on Sep 20, 2001 at 22:47 UTC
    Oh man,,,, I was all happy, and then I tried it, it worked great when running it from the prompt, but still just kind-of dies when done from the php app. I am hating this part! Here is where I am calling my perl script:

    $run="snmp-wdr -c mrtgtest $request &"; echo "<BR>COMMAND SENT: $run<BR>"; $setsys=system($run);

    DANG!
    I am wondering if it has something to do with the mysql commands in the perl script, it appears that snmp-wdr IS running, and that is DOES start using snmp, but the mysql commands in the script are not functioning. Maybe that is why it works perfect from prompt, but not from web system()??

    use hand while code!=$working bash $head
      It may be that PHP (or the web server) has set up a pipe between itself and the standard output of your script. If that's the case, then PHP (or the web server) won't think the page is finished until it gets end-of-file on that pipe. When you start a subprocess, it gets a copy of your script's stdout/stderr, which isn't released until the subprocess exits or explicitly closes those descriptors.

      Try closing stdout and stderr in the subprocess and see if it helps:

      $run="snmp-wdr -c mrtgtest $request > /dev/null 2>&1 &"; echo "<BR>COMMAND SENT: $run<BR>"; $setsys=system($run);