Click here to get back home

FAQ 8.12 How do I start a process in the background?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 8.12 How do I start a process in the background? PerlFAQ Server 04-03-2008
Posted by PerlFAQ Server on April 3, 2008, 3:03 pm
Please log in for more thread options
This is an excerpt from the latest version perlfaq8.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

8.12: 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 = sub { wait };

$SIG = '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 &")".



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Similar ThreadsPosted
FAQ 8.12: How do I start a process in the background? November 8, 2004, 6:03 pm
FAQ 8.12 How do I start a process in the background? March 13, 2005, 6:03 am
FAQ 8.12 How do I start a process in the background? June 11, 2005, 5:03 pm
FAQ 8.12 How do I start a process in the background? October 10, 2005, 4:03 pm
FAQ 8.12 How do I start a process in the background? November 5, 2005, 5:03 am
FAQ 8.12 How do I start a process in the background? November 29, 2005, 11:03 am
FAQ 8.12 How do I start a process in the background? January 18, 2006, 5:03 am
FAQ 8.12 How do I start a process in the background? September 17, 2006, 3:03 am
FAQ 8.12 How do I start a process in the background? November 25, 2006, 3:03 pm
FAQ 8.12 How do I start a process in the background? March 18, 2007, 3:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap