Skip to content
Snippets Groups Projects
Commit 7ca9e9c3 authored by Mark Harrison's avatar Mark Harrison
Browse files

Greatly simplify how run_command works. (refs #14)

The original intent was to fix bad behavior when a command doesn't exist. Upon
looking further into the implementation of the run_command function, I
realized that using open achieved the same effect (backticks but we get a
process id out of it) without a lot of code that didn't quite work when the
exec failed.

git-svn-id: https://labs.omniti.com/resmon/branches/resmon2@414 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55
parent ecc18db9
No related branches found
No related tags found
No related merge requests found
......@@ -37,23 +37,13 @@ sub run_command {
# Run a command just like `cmd`, but store the pid and stdout handles so
# they can be cleaned up later. For use with alarm().
my @cmd = @_;
pipe(my ($r, $w));
my $pid = fork();
if($pid) {
close($w);
$children{$pid} = $r;
my @lines = <$r>;
waitpid($pid, 0);
delete $children{$pid};
return join("", @lines);
} else {
eval {
open(STDOUT, ">&", $w);
close($r);
exec(@cmd);
};
exit();
}
my $pid = open(my $r, "-|", @cmd);
die "Can't run $cmd[0]: $!\n" unless defined($pid);
$children{$pid} = 1;
my @lines = <$r>;
delete $children{$pid};
close($r);
return join("", @lines);
}
1;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment