Linux – Using screen to monitor non-interactive scripts (or some other solution)

gnu-screenlinuxmonitoringscreen-sharing

I have some autonomous scripts that run commands on remote machines over ssh. These scripts rely on getting stdout, stderr, and the return code of each command run. I want to be able to monitor the progress of the scripts on each target machine so that I can see if something has hung and possibly intervene if necessary.

My initial idea was to have the scripts run commands in a screen session, so that the person monitoring could simply attach to the session with screen -x. However, it was hard to do that from a script since screen is an interactive program. I can send a command to the screen session with screen -S session -X stuff "command^M", but then I don't get the output and return code that I need back.

My second idea was to put script /path/to/log in ~/.bash_profile and log the entire session to a file. Then the monitoring person could simply tail the log file. However, this doesn't provide the interactivity that I was looking for.

Any ideas on how to solve this problem?

Best Answer

I use php for a shell script to manage my Minecraft server. I use the server log to get my replies. Here is the code I use to list who is online. I use tail to get the last few lines and parse them for the time to make sure the response is after when I sent the command.

#!/usr/bin/php
<?php
function send_cmd( $command )
{
        exec('screen -S minecraft -X stuff "`printf "\\r' . $command . '\\r"`"');
}

function who()
{
        if (!is_running())
        {
                echo 'Server is not running.' . CRLF;
                return 4;
        }

        // Get the current time and send the command
        $before = time() - 1;
        send_cmd('list');

        // Wait for the server to provide a response
        while(time() < $before + 5) {
                sleep(0.25);
                $result = exec('tail ' . __DIR__ . '/server.log | grep "\[INFO\] Connected players"');
                $stamp = strtotime(substr($result, 0, 19));
                if ($before <= $stamp)
                        break;
                unset($result);
        }

        if (isset($result))
        {
                echo $result . CRLF;
                echo 'Server responded in ' . ($stamp - $before) . ' seconds.' . CRLF;
                return 0;
        }
        else
        {
                echo 'Server did not respond.' . CRLF;
                return 4;
        }
}