LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell Scripting: Suppress error / Best 3 out of 5 (https://www.linuxquestions.org/questions/linux-general-1/shell-scripting-suppress-error-best-3-out-of-5-a-4175537633/)

thealmightyos 03-23-2015 02:29 PM

Shell Scripting: Suppress error / Best 3 out of 5
 
I am using few shell scripts to keep an eye on some important services on a windows box. Other solutions have proven to be unreliable (BigBrother decides it's not going to send an email on at a critical moment, automation on the windows box fails to run, etc). I, unfortunately, am not in a position where I can make changes to the windows box or BigBrother (if I was it would be a Linux Box and I would not have this problem).

First up, I am monitoring a number of services. The command is easy enough
Code:

net rpc service status servicename -I server -U user@domain%password
Even pass it though awk to give me a simple "ServiceName service is running / stopped / etc". However, this command has flaws. It has a one out of five chance to return the following:
Code:

Could not connect to server ###.###.###.###
Connection failed: NT_STATUS_BAD_NETWORK_NAME

However, if I try the command again, it works fine with no issues.

Here is my question: is there a way to suppress the error message and simply display output when it successfully runs? Secondly when it fails is there a way to make it run up to 5 times until success or and print a custom error message when it fails all 5 or 3 out of the 5 or something along those lines? Thank you for the input

T3RM1NVT0R 03-23-2015 02:37 PM

Here is what you can try:

Code:

net rpc service status servicename -I server -U user@domain%password 2&>1 /dev/null
    if [ $? -eq 0 ]
    then
    {
        echo "Command ran successfully" > /dev/null
    }
    else
    {
        for i in `seq 1 5`
        do
        {
              net rpc service status servicename -I server -U user@domain%password 2&>1 /dev/null
        }
        done
    }
    fi


suicidaleggroll 03-23-2015 02:38 PM

Dump the output into a variable and use the exit status to determine what to do with it (or whether to run it again)
Code:

num=0
output=$(net rpc service status servicename -I server -U user@domainpassword)
while [[ $? -ne 0 && $num -lt 5]]; do
  ((num++))
  output=$(net rpc service status servicename -I server -U user@domainpassword)
done
echo $output


thealmightyos 03-24-2015 09:43 AM

In the first example I get no output. The script shows all output being sent to /dev/null

In the second example I had to add a space after 5 and before ]] to get it to run. However, it still shows the error message. Am I missing something? do I need to put the output though ack, awk or something similar?

suicidaleggroll 03-24-2015 09:46 AM

Put a "2>&1" at the end of the net command to redirect stderr to stdout.

bimboleum 03-25-2015 10:53 AM

Hi,
To supress the error output, but keep the standard output redirect stderr (2) to /dev/null so shown below.
This ensures that "output" will only contain the words you want to see!

cheers
pete

pete hilton
saruman@ruvolo-hilton.org

Code:

num=0
output=$(net rpc service status servicename -I server -U user@domainpassword 2> /dev/null)
while [[ $? -ne 0 && $num -lt 5 ]]; do
  ((num++))
  output=$(net rpc service status servicename -I server -U user@domainpassword 2> /dev/null)
done
echo $output



All times are GMT -5. The time now is 02:22 PM.