Athanasius is correct,
& is a shell thing and doesn't do what you want (
man bash:
"If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0."). And you don't want the shell in the first place. Use a module, or just use
fork,
exec and
wait... this is what bash does anyway, except it doesn't
wait in your script, so you don't get
ping's exit code.
Something like that:
use strict;
use warnings;
use autodie;
use POSIX ':sys_wait_h';
my @sites = qw(
www.perlmonks.org
www.stackoverflow.com
www.whitehouse.gov
);
open my $stdout, '>&', \*STDOUT; # dup STDOUT
open STDOUT, '>', '/dev/null'; # to make ping shut up
my %kids;
for my $site (@sites) {
my $pid = fork;
$pid == 0 and exec 'ping', '-c', '3', $site;
$kids{$pid} = $site;
}
while ( ( my $kid = wait ) != -1 ) {
print $stdout "$kids{$kid} returned $?\n";
}
Output:
www.stackoverflow.com returned 0
www.perlmonks.org returned 0
www.whitehouse.gov returned 0
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.