(This is a reply to Re^5)
Your suggestions have provided the right answer! Here are two scripts; the first hangs until the sound file stops playing, and then displays a confirmation message.
#!/usr/bin/perl use strict; use diagnostics; use warnings; use IPC::Run qw/start/; use POSIX ":sys_wait_h"; my ($h, $pid, $result); # Play the sound file $h = start ['play','-q','/home/ra/Desktop/trek3.mp3']; # Get the process ID (there's only one) foreach my $kid (@{ $h->{KIDS} }) { $pid = $kid->{PID}; } # Wait for the sound file to stop playing, and then display a message # Option 1: scripts hangs, and then correctly displays a message # as soon as the sound file stops playing sleep 5; waitpid($pid, 0); print "finished!\n"; # Tidy up $h->signal('INT'); $h->finish;
The second checks once a second, and prints a confirmation message within a second of the sound file stopping.
#!/usr/bin/perl use strict; use diagnostics; use warnings; use IPC::Run qw/start/; use POSIX ":sys_wait_h"; my ($h, $pid, $result); # Play the sound file $h = start ['play','-q','/home/ra/Desktop/trek3.mp3']; # Get the process ID (there's only one) foreach my $kid (@{ $h->{KIDS} }) { $pid = $kid->{PID}; } # Wait for the sound file to stop playing, and then display a message # Option 2: correctly displays a message within a second of the sound # file stopping playing do { sleep 1; waitpid($pid, WNOHANG); print "waiting...\n"; } until (! (kill 0, $pid)); print "finished!\n"; # Tidy up $h->signal('INT'); $h->finish;
In reply to Re^4: Killing a child process
by Anonymous Monk
in thread Killing a child process
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |