LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-23-2015, 02:29 PM   #1
thealmightyos
Member
 
Registered: Mar 2009
Distribution: CentOS 6.5 / 7
Posts: 119

Rep: Reputation: 1
Post 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
 
Old 03-23-2015, 02:37 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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
 
Old 03-23-2015, 02:38 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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
 
Old 03-24-2015, 09:43 AM   #4
thealmightyos
Member
 
Registered: Mar 2009
Distribution: CentOS 6.5 / 7
Posts: 119

Original Poster
Rep: Reputation: 1
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?
 
Old 03-24-2015, 09:46 AM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

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

Last edited by suicidaleggroll; 03-24-2015 at 09:49 AM.
 
Old 03-25-2015, 10:53 AM   #6
bimboleum
Member
 
Registered: Oct 2009
Posts: 111

Rep: Reputation: 23
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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help With Shell Scripting Error. mrm5102 Programming 15 09-28-2011 08:28 AM
C-shell scripting syntax error chrisjorg Programming 1 04-06-2011 03:47 AM
shell scripting error shouup Programming 8 09-08-2005 12:38 PM
Shell scripting error bindweb Programming 4 09-30-2004 10:50 AM
bash scripting - suppress error messages? brian0918 Programming 2 06-19-2003 11:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration