I have a bash script that tails a file for a string that contains either "CONNECT" or "DISCONNECT". Once such a string is found that string is then piped to a php sript.
Here is the bash script:
tail -f -n 1 /var/log/connections | grep -P -0 --line-buffered "\bCONNECTED\b|\bDISCONNECTED\b" | php -f $SCRIPT_DIR/connections.php
And here is the php script:
#!/usr/bin/php
<?php
while ( false !== ( $connection_status = fgets ( STDIN ) ) )
{
$get_status = preg_match ( "/\bCONNECTED\b|\bDISCONNECTED\b/", @$connection_status, $status_match ) ;
foreach ( $status_match as $status )
{
switch ( $status )
{
case "CONNECTED": //If the string that got passed to this script (from the BASH script) contains CONNECTED
{
print ( "we are connected\r\n" ) ;
}
case "DISCONNECTED": //If the string that got passed to this script (from the BASH script) contains DISCONNECTED
{
print ( "we are disconnected\r\n" ) ;
}
}
}
}
?>
DISCONNECT works as expected but with CONNECT it returns both "we are connected" and "we are disconnected"