icon Top 9 categories map      RocketAware > Perl >

How do I start a process in the background?

Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.



Home

Search Perl pages


Subjects

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Math
Integer, Floating point, Matrix, Statistics, Boolean, ...

Processing
Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Communications
Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...

    

How do I start a process in the background?

You could use

    system("cmd &")

or you could use fork as documented in fork, with further examples in the perlipc manpage. 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 opening a pipe (see open) 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 };

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


Source: Perl FAQ: System Interaction
Copyright: Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
Next: How do I trap control characters/signals?

Previous: How do I decode encrypted password files?



(Corrections, notes, and links courtesy of RocketAware.com)


[Overview Topics]

Up to: Process Creation and Control




Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perlfaq8/How_do_I_start_a_process_in_the_.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments