Suppose I'm running a backticks or system command through a script that I've run from the command line. Is there anyway for me to stop that command midstream and have the script continue?
The application in question is a very simple MP3 shuffle player. What I want to be able to do is go to the next song while the current one is playing. Read more for the code.
#!/usr/bin/perl
use Data::Dumper;
open(PLAYLIST,"</somedir/myplaylist.lst");
my @songs;
while (<PLAYLIST>) {
chomp();
push(@songs,$_);
}
my $count = scalar(@songs);
print "We found $count songs\n";
while (scalar(@songs) > 0) {
my $song_num = rand(scalar(@songs));
print "*** Playing $songs[$song_num]\n";
`mpg321 '$songs[$song_num]'`;
splice(@songs,$song_num,1);
}
If it makes any difference, this is Perl 5.8.5 on SuSE Linux 9.2.
UPDATE: After taking my first steps into the larger world of fork/exec, I have something that works to the best that I can see. Read on for the new version. I'll probably eventually update it to not use ^C to get the next song so it's easier to stop the music. Thanks to RazorbladeBidet for gently pushing me to learn fork!
#!/usr/bin/perl
my $song_num;
my @songs;
my $pid;
sub catch_next {
my $signame = shift;
print "Going to next song!\n";
my $count = kill(15,$pid);
}
sub reaper {
$waitedpid = wait;
$SIG{CHLD} = \&reaper;
}
open(PLAYLIST,"</somedir/myplaylist.lst");
while (<PLAYLIST>) {
chomp();
push(@songs,$_);
}
$SIG{INT} = \&catch_next;
$SIG{CHLD} = \&reaper;
my $count = scalar(@songs);
print "We found $count songs\n";
while (scalar(@songs) > 0) {
$song_num = rand(scalar(@songs));
my $song = $songs[$song_num];
splice(@songs,$song_num,1);
print "*** Playing $song\n";
unless ($pid = fork) {
exec("mpg321", "$song");
}
waitpid($pid,0);
}
UPDATE 2: Thanks to bluto for alerting me that I don't need a seperate SIGCHLD handler. The final code is below:
#!/usr/bin/perl
my $song_num;
my @songs;
my $pid;
sub catch_next {
my $signame = shift;
print "Going to next song!\n";
my $count = kill(15,$pid);
}
open(PLAYLIST,"</somedir/myplaylist.lst");
while (<PLAYLIST>) {
chomp();
push(@songs,$_);
}
$SIG{INT} = \&catch_next;
my $count = scalar(@songs);
print "We found $count songs\n";
while (scalar(@songs) > 0) {
$song_num = rand(scalar(@songs));
my $song = $songs[$song_num];
splice(@songs,$song_num,1);
print "*** Playing $song\n";
unless ($pid = fork) {
exec("mpg321", "$song");
}
waitpid($pid,0);
}