views:

285

answers:

4

Can somebody please tell me how to fire-and-forget a process in Perl? I've already looked at ruby: how to fire and forget a subprocess? for doing the same in Ruby.

+1  A: 

If you want to daemonize your Perl script I'd looking into Proc::Daemon.

Mark
Actually, I don't want to daemonize it, I just need to launch a watchdog script in the background for the duration of an execution of my main script. And I have only basic Perl 5.6.0 to work with, because this is executing on an embedded system with severely limited memory and I have pretty much no modules to work with.
Alex Marshall
A: 

Well you shouldn't use system() as I understand this call will wait for the return of the function before continuing execution. I would simply shell out to linux and start the process that way, though keep in mind this way will envoke a new copy of the shell and so if you're looking for performance I wouldnt do this in a loop for example:

process_name > /dev/null 2>&1 &;

That will start the process redirecting STDOUT and STDERR to /dev/null.

Matt1776
+12  A: 

From perlfaq8's answer to How do I start a process in the background?


Several modules can start other processes that do not block your Perl program. You can use IPC::Open3, Parallel::Jobs, IPC::Run, and some of the POE modules. See CPAN for more details.

You could also use

system("cmd &")

or you could use fork as documented in "fork" in perlfunc, with further examples in perlipc. Some things to be aware of, if you're on a Unix- like system:

STDIN, STDOUT, and STDERR are shared

Both the main process and the backgrounded one (the "child" process) share the same STDIN, STDOUT and STDERR filehandles. If both try to access them at once, strange things can happen. You may want to close or reopen these for the child. You can get around this with "open"ing a pipe (see "open" in perlfunc) but on some systems this means that the child process cannot outlive the parent.

Signals

You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too. SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is sent when you write to a filehandle whose child process has closed (an untrapped SIGPIPE can cause your program to silently die). This is not an issue with "system("cmd&")".

Zombies

You have to be prepared to "reap" the child process when it finishes.

   $SIG{CHLD} = sub { wait };

   $SIG{CHLD} = 'IGNORE';

You can also use a double fork. You immediately wait() for your first child, and the init daemon will wait() for your grandchild once it exits.

   unless ($pid = fork) {
       unless (fork) {
           exec "what you really wanna do";
           die "exec failed!";
       }
       exit 0;
   }
   waitpid($pid, 0);

See "Signals" in perlipc for other examples of code to do this. Zombies are not an issue with "system("prog &")".

brian d foy
Thank you very much, I hadn't thought to use the background shell operator in the command. That did the trick. Unfortunately I can't use the others because I'm working on a severely limited system with almost no modules.
Alex Marshall
Matt1776
A: 

If you want the watchdog "child" process to go away when the "parent" process exits then you don't really want the parent process to "fire and forget" the child process. You either need to fork (as described by brian d foy) or pass the PID of the parent to the child (so the latter can poll the process table - not recommended).

Peter
Peter