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.
4 Answers
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 &")".

- 21,510
- 5
- 55
- 89

- 129,424
- 31
- 207
- 592
-
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 Jan 25 '10 at 20:23
-
Ah that's right, if you issue a system() command with the '&' char it returns right away, thus the back-tick operator method I had suggested was not so good - better to use Perl's system call rather than call a whole new copy of the shell. – Jan 25 '10 at 23:28
If you want to daemonize your Perl script I'd looking into Proc::Daemon.

- 106,305
- 20
- 172
- 230
-
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 Jan 25 '10 at 16:57
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.

- 129,424
- 31
- 207
- 592
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).

- 2,526
- 1
- 23
- 32
-
Actually, running a watchdog script as a child process is probably a bad idea since presumably the watchdog is supposed to do something if the main script dies unexpectedly (but if the parent dies then so do all its children). Another way to go is to have the "main process" set up a named pipe and write "heartbeat" messages to it + a "done" message on exit, while the watchdog (launched in true "fire and forget" mode via system("cmd &")) reads from the named pipe and notices when either the "done" message arrives or the "heartbeat" messages stop coming. – Peter Jan 25 '10 at 22:34