Hi Monks,

I know it's Friday but I hope there is someone out there can help me with my situation. I have a cgi file that needs to execute another file without having the browser wait for the outcome of the execution. Here is a little diagram:
browser->request->cgi->execute another program
                    |
                  return submitted successful message and 
                  close connection without waiting for the                 
                  other process

The code that I have is not working like the way I want because when I make the request to the cgi file, the browser waits for all processes to finish before closing the connection. I just need to execute "launch" and I do not need it to wait for it to finish before sending back the "successful submit" message. I think I am really close and this will probably involve forking and the use of exec() or even system() but this is what I have so far:

I have edited the code according to runrig's suggestion but I'm not sure if I did it right.
chdir '/' or die "Can't chdir to /: $!"; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!"; my $pid = fork(); if (not defined $pid) { print "resources not available.\n"; } elsif ($pid == 0) { #this is where I launch the other executable system("/usr/local/bin/launch myfile &") or die("Cannot execute la +unch: $!\n"); exit; } else { setsid or die "Can't start a new session: $!"; open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; #this is the return message print "Your file has been sent successfully. You will be notified + via email once it has been completed. Thank you."; print end_html(); exit; }

Any suggestions would be greatly appreciated. Thanks in advance.

newb

In reply to Fork() and exec() by axl163

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.