in reply to Re: Executing a program, displaying some status, and killing it
in thread Executing a program, displaying some status, and killing it

I realize you were probably doing a quick port, but there are a few things I would like to point out.

Fixing these, I would probably write it as follows:

my $child = fork; if ($child) { # I am in the parent...wait then kill for (1..10) { sleep 1; print "."; } kill 9, $child; } else { # Run the background program exec '/path/to/program > /dev/null 2>&1'; }

Update: an alternative method that avoids explicit forking, and perhaps looks a bit more Perlish:

my $pid = open my $cmdfh, "-|", '/path/to/program' or die "Cannot fork: $!\n"; for ( 1 .. 10 ) { print "."; sleep 1; } kill 9, $pid;

Replies are listed 'Best First'.
Re^3: Executing a program, displaying some status, and killing it
by Spida (Acolyte) on Dec 12, 2004 at 16:58 UTC
    Both versions work, but not the way I want... the status output (print ".") appears only after the background-program has been killed, which makes it pretty useless...
      the status output (print ".") appears only after the background-program has been killed

      That's probably a buffering problem. The short answer is to set $|, the long answer is to read Suffering from Buffering.