1

I have a script that monitors game servers. It is written in bash. However I am attempting to put together an advanced monitor to pick up if the server has locked up. This requires a small python script to query the server and it returns either OK, ERROR 1 or ERROR 2. What is the best way to capture the output? Currently with the script the variable is empty. I can confirm the script is running OK and returning a message.

if [ -f serverquery.py ];then
    echo -e "[\e[0;36m INFO\e[0;39m ] Running advanced monitoring serverquery.py"
    echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Running advanced monitoring serverquery.py" >> ${logfile}
    serverquery=`./serverquery.py 127.0.0.1 27015`
    if [ "${serverquery}" = "ERROR 1" ];then
        echo -e "[\e[0;31m FAIL\e[0;39m ] ${servicename}: serverquery.py Monitor detected ${servername} has crashed!"
        echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py Monitor detected ${servername} has crashed!" >> ${logfile}
        fn_restartserver
    elif [ "${serverquery}" = "ERROR 2" ];then
        echo -e "[\e[0;31m FAIL\e[0;39m ] ${servicename}: serverquery.py Monitor detected ${servername} has crashed!"
        echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py Monitor detected ${servername} has crashed!" >> ${logfile}
        fn_restartserver
    fi
fi

Thank you

  • 1
    You are missing a fi, but that might just be a copy&paste error. Otherwise is serverquery.py executabel? And is the python script printing to STDOUT or STDERR? – ahilsend Jul 26 '13 at 11:51
  • 1
    To color text in your script see mywiki.wooledge.org/BashFAQ/037?highlight=%28color%29 When it comes to test if a file exists use if [[ -f serverquery.py ]]; then ... ; fi When it comes to string comparison use if [[ "${serverquery}" == "ERROR 1" ]]; then ... ; fi – Valentin Bajrami Jul 26 '13 at 12:25
  • Yeah copy&paste error :). serverquery.py is an executable that when run will return OK or ERROR. – dgibbs Jul 26 '13 at 12:28
  • 1
    Rather than repeating code, use a compound conditional: if [[ "${serverquery}" = "ERROR 1" || "${serverquery}" = "ERROR 2" ]] (or you could use a case statement with compound conditions for some of the cases). – Dennis Williamson Jul 26 '13 at 13:39
2

added 2>&1 to the end and it is now working

e.g

serverquery=$(./serverquery.py 127.0.0.1 27015 2>&1)

  • 1
    The ./serverquery.py does not really make sense. You should put serverquery in your PATH and use it as in serverquery=$(serverquery IP port 2>&1) This is much safer for the fact that you don't have to bother about relative paths. – Valentin Bajrami Jul 26 '13 at 14:56
1

If you have access to edit serverquery.py, I would change the exit value for the error conditions and then simply check the value of $? after running it. If not, try changing your if conditional on output check to the format suggested by val0x00ff: if [[ "${serverquery}" == "ERROR 1" ]]; then ... fi.

  • The issue I appear to be getting is that when I echo ${serverquery} it is blank – dgibbs Jul 26 '13 at 12:44
  • 1
    Is serversquery.py printing to STDOUT or STDERR? – John Jul 26 '13 at 12:48
  • 1
    @dgibbs See mywiki.wooledge.org/BashFAQ/002 why ${serverquery} is returning blank. – Valentin Bajrami Jul 26 '13 at 13:07
  • added 2>&1 and it is now working e.g serverquery=./serverquery.py 127.0.0.1 27015 2>&1 – dgibbs Jul 26 '13 at 14:09
  • So it was printing to STDERR. – John Jul 26 '13 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.