Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

CGI - run script without waiting

by Yaerox (Scribe)
on Apr 15, 2014 at 10:51 UTC ( #1082332=perlquestion: print w/replies, xml ) Need Help??

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

Hey guys, I'm facing a little problem with Perl and CGI. I got a homepage with a button, on-click I'm submitting the form values and running a perl script. In this perl script I need to run a system command without waiting for the end of the command!

I did a workaround on windows local machine by finding a system command without waiting. I found out that I can run commands with a leading "start " without waiting. On windows this works fine. Reproduce: Create an perl script:

system ( "start Example.bat 30" ); # 30 is the time in seconds to slee +p print "Finished";
Create an Example.bat:
timeout /t %1 :setError Exit /B 5


Now I tried to get this done on an XAMPP webserver. When I'm clicking my button I start the first perl script which is doing a system("start perl -w just_another_perl_script.pl") and printing the complete html-site. But my browser symbol which shows if a page is loading, and this "Inspect Element > Network" in Google Chrome Browsers is showing that the site is still loading.

The site shown when I started the script is just a html-link and i can click it and I'm able to get back while the scripts are still running ... but when I try to use those meta-tags to redirect isntantly to another page after pressing a button the site waits till the scripts finished like the status-symbol in browser is doing.

Ofc I did some research to find out what the problem is, and I read pretty often that I should use modules like Proc::Daemon, but If I don't have to fork/build own processes, I don't want it! Threads I looked up (just a few examples):
http://stackoverflow.com/questions/2668124/how-can-i-run-an-external-process-without-waiting-for-it-to-finish-from-a-perl-c
But this Proc::Daemon Tool can't be used on windows ...

Can someone tell me maybe how to fix my problem in case there is a chance to fix it, otherway if I have to use such modules like Proc::Daemon why Webservers (the Apache) is working like this? I mean if I have to go such a way, I want to know why I have to ...

Regards

Replies are listed 'Best First'.
Re: CGI - run script without waiting
by scorpio17 (Canon) on Apr 15, 2014 at 14:05 UTC
Re: CGI - run script without waiting
by Anonymous Monk on Apr 15, 2014 at 13:54 UTC
    I would create a separate daemon process which is responsible for forking and running those commands specifically so that you can limit the number of processes that might in this way be running at the same time. Without this precaution, your system could be used to intentionally or unintentionally "fork bomb" your system.
Re: CGI - run script without waiting
by Yaerox (Scribe) on Apr 15, 2014 at 14:36 UTC
    First of all thanks for replying. I'm actual raging about myself, because I'm refreshing this thread like every 2 minutes but I didn't saw the replies :/ @Anonymous Monk: I already thought about something like that but this would only be a last option for me because i don' t have to be afraid of a "fork bomb" (this system is only in our internal domain without any connection to the web), I'd more like to do this the "on event way" because polling got a few negative aspects I don't like. @scorpio17: I already wrote this thread. Doesn't matter if I use the Proc::Daemon Module or doing this with my own hands, my web-server still shows the same behavior, waiting for scripts end. $| = 1 or closing/redirecting standard input/output/error doesn't help either - same behavior. I'm not sure if I didn't understood everything in this article of Watching long processes through CGI, but I don't care about refreshing and watching things like that. I just need to click a button -> start cgi script which starts system commands WITHOUT waiting for the end -> saying browser "hey I finished" (while the system commands are still running. I'll give this article another try but I'd appreciate if you could tell me if I'm wrong with my view of the article as far as you understood more of it. Regards.
      I'm not sure if I didn't understood everything in this article of Watching long processes through CGI, but I don't care about refreshing and watching things like that. I just need to click a button -> start cgi script which starts system commands WITHOUT waiting for the end -> saying browser "hey I finished" (while the system commands are still running.

      What you probably want to do, is fork off the desired script, with SIG(CHLD) ignored, so it won't wait. Then, you close STDOUT and other filehandles in your cgi script, and the web server should close it off.

      See Merlyn's Column 20

      or this simpler example:

      #!/usr/bin/perl use warnings; use strict; $| = 1; # need either this or to explicitly flush stdout, etc. # before forking print "Content-type: text/plain\n\n"; print "Going to start the fork now\n"; if ( !defined(my $pid = fork())) { print STDERR "fork error\n"; exit(1); } elsif ($pid == 0) { # child close(STDOUT);close(STDIN);close(STDERR); exec('./fork-long-process-test-process'); # lengthy processing } else { # parent print "forked child \$pid= $pid\n"; exit 0; }

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        I'm actual not at work anymore that I could try this but tomorrow I'll try this for sure. I really appreciate your help. Thanks

        I had some similar code but I must have done some mistakes. Your codes seems perfect on the first look. Did a few tests and always the exactly expected result.

        I'd now try to go on with my project getting this running. In case that I'll note something is not doing what expected, may I just reply here? :-)

        Thanks you Sir!

Re: CGI - run script without waiting
by Anonymous Monk on Apr 15, 2014 at 17:58 UTC

      Firstly thanks for the reply, in case that I had the same behavior on Win/Linux, and it looks like the script by zentara works perfectly on Linux (because fork is implemented in two different ways in Win/Linux - my actual information's) I'll give it a try. I'd prefer Linux anyway.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2019-12-09 05:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?