Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question
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 –  val0x00ff 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 Answers 2

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.

share|improve this answer
    
Thanks I will try this later –  dgibbs Jul 26 '13 at 12:42
    
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. –  val0x00ff 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

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

e.g

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

share|improve this answer
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. –  val0x00ff Jul 26 '13 at 14:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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