in reply to CGI and Sleep?

First off, you should have parentheses around the @list or else this is not valid syntax. Since we don't have a correct code snippet, it's tough to see what the problem is. Further, are you using strict and warnings? If so, you may get some useful information in your error log that would guide you. My main question, though, is "how are you setting $time"? I know of no reason why it wouldn't operate properly via the Web.

Frankly, though, I've never used this with a CGI script. If a script takes too long to return an answer, the server may issue a 408 Request Time-out status to the browser and kill the process. If you provide us with a bit more code, we may be able to answer your question or provide you with an alternate solution.

Cheers,
Ovid

Vote for paco!

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: (Ovid) Re: CGI and Sleep?
by jellisii (Novice) on Jun 26, 2001 at 04:18 UTC
    I'm almost ashamed to show how amature my code is, but here it is...
    #!/usr/bin/perl -w close STDOUT; @playlist = `cat ./playlist.txt`; $totalnum = scalar(@playlist); foreach $i (@playlist) { # a random number for the randomizer. $random = int(rand $totalnum); # use that random number to determine a file to play. $filename = $playlist[$random]; # format the file name so I can *USE* it. # the first regex escapes symbols for me. the second, spaces. $filename =~ s/(')|(&)|(\()|(\))/\\$+/g; $filename =~ s/\s/\\\ /g; # play the file, throw that process into the bg system("/usr/local/bin/ogg123 -d oss -q $filename &"); # write the PID and filename to a file # need this sleep in here to make shure the player starts before I t +ry # to write the PID sleep 2; system("ps -U root --format pid --format fname| grep ogg123> ./playe +r.pid"); open(PLAYING, ">./nowplaying.txt"); print PLAYING $filename; close PLAYING; # decriment the total number of files left to play. $totalnum--; # remove the filename from the list. splice @playlist, $random, 1; # get the information for the file that is playing. $playtime = `ogginfo $filename| grep length=`; # get the total playtime in secs for the file. $playtime =~ s/length=//; # pause for that many seconds, as to not play multiple files # simultaneously :) sleep $time; }

    I shut off STDOUT so the CGI calling this script won't wait for this to execute.

      This may be a typo. strict would have caught it.

      sleep $playtime;

      Here's a very quick rewrite. It's untested, but it's a little nicer, in my opinion.

      #!/usr/bin/perl -w use strict; close STDOUT; open(LIST, 'playlist.txt') or die "Can't open playlist: $!"; my @playlist = <LIST>; close LIST; while (@playlist) { my $pos = rand @playlist; my $filename = playlist[$pos]; system("/usr/local/bin/ogg123", '-d', 'oss', 'q', $filename, '&'); sleep 2; foreach (`ps -U root --format pid fname`) { next unless /ogg123/; open(PID, '>player.pid') or warn "Can't log pid ($_): $!"; print PID $_; close PID; last; } open(PLAYING, ">nowplaying.txt") or warn "Can't log filename $file +name: $!"; print PLAYING $filename; close PLAYING; splice @playlist, $pos, 1; my $playtime; foreach (`ogginfo $filename`) next unless /length=(.+)$/; $playtime = $1; last; } sleep $playtime; }