# forks then changes working directory and executes a command sub chdir_and_system { my ( $dir, @exec_args ) = @_; my $pid = fork; die "couldn't fork: $!" unless defined $pid; unless ($pid) { # this is the child chdir($dir); # open STDOUT, ">/dev/null"; # silence subprocess output # open STDERR, ">/dev/null"; # silence subprocess stderr system(@exec_args) == 0 or die "system @exec_args failed"; } wait; die "child error $?" if ($?); }