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); }

In reply to Can I stop a backtick command but continue the script? by DentArthurDent

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.