SkipHuffman has asked for the wisdom of the Perl Monks concerning the following question:

I have Perl CGI on a server that runs a local to the server program. I would like to write a separate piece of code that can shut it down.

The first process is a media player (mplayer, to be precise)that is playing sound through my home sound system. Currently I keep the script running in a browser window (on any other computer connected to my network), and can stop the sound by closing that window.

But I also have a cron job that starts the media player in the morning to play a morning news stream. The only way I have to stop that job is to ssh into the maching and do a kill.

I would like to be able to stop this sound from a browser window. I would either like to be able to send a "q" to running mplayer jobs, or run "killall mplayer" and have it shut stuff down.

I would also like to be able to close the sound from a system different than the one that started it.

Any suggestions?

Skip

Replies are listed 'Best First'.
Re: CGI Process Control
by graff (Chancellor) on Dec 24, 2007 at 18:13 UTC
    Unless the cron job that starts the morning news stream is owned by the same user account that is running httpd on that machine, a CGI process to kill the playback would need root privilege (or at least appropriate sudo permission). And as a web-based interface, that is not an attractive solution.

    My first inclination would be to tailor the cron job so that it not only launches the audio stream player, but also watches (say, at 1 or 2 sec intervals) for the presence of a "stop.playback" file in some designated path. (This means the playback has to be launched as a background process, so the main script being run by cron can continue to operate.) Then the web interaction simply consists of creating the "stop.playback" file on demand, and the cron job script, on seeing this file, kills its child playback process, deletes the file, and exits.

    (update: assuming it can happen that the child playback finishes normally (or can be said to be "done") before some unrelated CGI action creates the "stop.playback" file, the cron script would need to check for this condition as well, and exit. You probably don't want today's instance to still be running when cron starts tomorrow's instance.)

      Typically the user of CGI is "nobody" right? Can "nobody" run a cron job? I did add some code to my CGI to shut down the sound, and it works fine on streams started from Apache, but as you implied, the cron started stream is unaffected.

      I don't like polling for a file, seems inelegant. Rather have a push process.

      The stream that I run from cron is permanent(or nearly so). It is the feed from my local NPR station, plays news until 9:00, then goes into really bad classical music, probably until 4:00.

      Edit: Ok, answered my own question. Yes, "nobody" appears to be able to run a cron job. I moved my timed feed to be owned by "nobody" (discovered that I had to su into root, then from root to nobody). Hopefully I will be able to take better control now.

      Skip