I wrote a little php command line script that checks on mpg123 and restarts it if it starts to die (ie- the stream goes offline for 15 minutes, it will start back when the stream comes back).
I have a simple question, I want to do check and see if it's already running, and if it is, to kill the other one.
Here's the code, feel free to use this yourself, it's my first attempt at a php daemon process:
#!/usr/local/bin/php -q
<?php
function usage()
{
echo 'phpCast 1.0 - by Stephen VanDyke stephen@opido.com'."\n";
echo 'usage: phpcast [stream server]'."\n";
echo ' phpcast -f [/path/to/list-of-streams.pls]'."\n";
exit;
}
if($argv[1] == '-h' || $argv[1] == '' || !isset($argv[1]) || (isset($argv[2]) && $argv[1] != '-f'))
{
usage();
}
$ref = $argv[2];
$type = 'playlist';
if($argv[2] == '')
{
$ref = $argv[1];
$type ='';
}
function mpg123($ref,$type){
$cmd = 'mpg123 -b 2048 -Z ';
if($type=='playlist')
{
if(!file_exists($ref))
{
$error = 'Could not open playlist: '.$ref."\n";
}
else
{
$cmd .= '-@ '.$ref;
}
}
else
{
$ref_parts = parse_url($ref);
$fp = fsockopen($ref_parts['host'],$ref_parts['port'],$errno,$errstr,3);
if(!$fp)
{
$error = 'Could not connect to server: '.$ref."\n";
}
else
{
$cmd .= $ref;
}
}
$cmd .= ' &> /dev/null &';
if(!isset($error))
{
#echo $cmd."\n";
exec('killall -9 mpg123');
exec($cmd);
}
else
{
echo $error;
exit;
}
}
while(true)
{
$is_running = exec('lsof -t /dev/dsp');
if($is_running == '')
{
mpg123($ref,$type);
}
sleep(5);
}
?>