my $pipe; open($pipe, '-|', 'ip -o xfrm monitor') || die "couldn't start sub-command"; my $pid = fork(); if (! defined $pid) { die "couldn't fork"; elsif ($pid > 0) { # write $pid to .pid file cleanup(); exit(0); ## problem happens HERE } ## now in sub-process close(STDIN); close(STDOUT); close(STDERR); ... while (my $line = <$pipe>) { ... } close($pipe); cleanup2(); exit(0);