• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

process handling

I have a perl cgi script which executes a program using `` what I would like to do, is start the process and then immediatly send the response back to the user, without waiting for the process to finish.

In other words: the user makes an http request, the script starts a process, sends back the response, and some time later the process finishes (without anyone really knowing about it).

How can I do this?

  • 3
  • 2
1 Solution
You could merely append an ampersand (&) to the end of the system call:

  `/some/process arg1 arg2 &`;

This is exactly what fork() is for.

$x=fork();  #make two copies of the running process
if ($x > 0) { #the maker of the copy gets the
              #pid of the new one
  print ...   #whatever you want to tell the user
  close(STDOUT); #ensure that the web server knows
  }           #we are done
else {
  #we are the second copy
  ... do whatever he wants to do, no one will know

In the case where the other process is as described by japhyrpi I somewhat agree that the & method works, if the OS is one that supports the & method.  

The things I do not like about this is that: 1) it does not take advantage of multi-threading where implemented, 2) it is OS specific, 3) it does not allow the rest of the work to be done in the same perl script.

But, I do agree it will work with these restrictions.
jhurst - well, fork() is OS dependent too.  ActivePerl has only recently provided support for it.
correct.  However, fork() is defined in perl, the & is not.  Also, most decent multi-tasking operating systems implement fork() therefore it makes its way into perl there too.  Now, MS products are of course a different issue.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now