LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-21-2015, 04:37 PM   #1
cojafoji
LQ Newbie
 
Registered: Jan 2015
Posts: 11

Rep: Reputation: Disabled
grep output of command to return 1 or 0 for if then statement


So, I'm new here. Was thrown into the deepend (read: shallow pool) of linux administration of a few centos 6.5 servers in October, and I've been pretty slow on the uptake. Years of Windows administration and desktop support will do that I guess.

So we have qmail on our prod servers that send out messages for our webapps. Postfix failed last night on a prod server, and it occurred to me that it's happened a few times since I've started, and instead of waiting for clients to tell us, I should be more proactive. I came up with/begged/borrowed/stole a quick script to check to see if postscript was running, and if it was, restart it, and contact us. This was to be run from jenkins.

Quote:
if (( $(ps -ef | grep -v grep | grep postfix | wc -l) > 0 ))
then echo 'Mail is up.'
else echo 'Mail is down! Attempting to restart service...'
service postfix restart
fi
The question was then raised about an unresponsive process (zombies!), and I figured it might be a better idea to use postqueue -q, or mailq to either see a Mailbox is empty, or look for fatal if postfix is off or unresponsive. This is where I ran out of coffee and went over the deep end.

To get this to work, I thought it would be a good idea to run two scripts. One to check if the mail queue is empty, and if not, send up a flag. The next was to run a few minutes later to check and see if the mail queue was throwing up a fatal error, and if it was, throw up a second flag, which sent out an email from jenkins. Incidentally, if the mailqueue were clogged, this would also trigger an alert. Our mailqueue doesn't get a lot of traffic.

First command. Returns then and else statement without issue.
Quote:
if ((mailq | grep 'empty') < 0 ); then echo 'Mail is up.'; else echo 'Mail queue is not empty. Please check mail server'; fi
Second one... Not so much.
Quote:
if (mailq | grep 'fatal') < 1; then echo 'Mail is up.'; else echo 'Mail queue is not empty. Please check mail server'; fi
You can probably suss out what I'm trying to do here, even though I'm not doing it right. Any ideas? I know this is newbie stuff, and I appreciate your taking the time to read this people.
 
Old 01-21-2015, 04:52 PM   #2
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
It looks you're trying to run a if statement based on whether grep fails or suceeds.
Remember, a successful command will output a error code of 0
If it fails, sometimes 1, or other various codes based on the error.
You can view the error code of the previous command with $?

So to test if mailq output has the string 'empty' in it:
Code:
if [ "$(mailq | grep 'empty' &>/dev/null; echo $?)" == 0 ]; then echo success; else echo failure; fi
Notice with your example, if it contains fatal, it will suceed. I am presuming if the string contains 'fatal' in it, the mail server is actually down?
Code:
if [ "$(mailq | grep 'fatal' &>/dev/null; echo $?)" != 0 ]; then echo 'Mail is up.'; else echo 'Mail queue is not empty. Please check mail server'; fi
Here is the string contains fatal in it, it will ouput a 0, which we take to mean the server is down. If it doesn't output a fatal, it will ouput a 1, so we know the server is up.

This is not the only way to do it of course.

Last edited by Miati; 01-21-2015 at 05:00 PM.
 
Old 01-21-2015, 07:06 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
In your second example, replace your single paren.s with doubles. ((<exp>)) is NOT the same as (<exp>).
 
Old 01-21-2015, 11:27 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Or we could just let 'if' do the heavy lifting:
Code:
if mailq | grep -q 'fatal'
then
  <your stuff here>
else
  <your other stuff>
fi
 
Old 01-22-2015, 10:44 AM   #5
cojafoji
LQ Newbie
 
Registered: Jan 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
Miati, thank you not only for your help, but teaching me something new! Had no idea you could use echo $? for that. PTrenholme, grail, thanks for replying!
 
Old 01-22-2015, 11:20 AM   #6
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
Quote:
Originally Posted by cojafoji View Post
Miati, thank you not only for your help, but teaching me something new! Had no idea you could use echo $? for that. PTrenholme, grail, thanks for replying!
I'm glad it helped, but I greatly suggest you do something to help you be able to use bash more effectively.

Read the man page of bash (yes - RTM)
There is a abundant amount of information that is very useful when writing bash scripts
and just using bash in general. Reading over the sections I listed below will save you months
of browsing the web for why does n do this. I use most of the below daily (or even by the minute)
You can use / to search within man pages & lowercase n to go next in search (N previous)
Some things I greatly suggest examining is
  • Pipelines
  • Lists
  • Quoting
  • Special Parameters
  • Command Substitution
  • REDIRECTION
  • FUNCTIONS
  • CONDITIONAL EXPRESSIONS (if, while, for)

If any of it is confusing, feel free to ask for clarification.
I also always suggest this as well

Last edited by Miati; 01-22-2015 at 11:24 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] return value for find or grep for use as an if statement condition IneptCoder Linux - Newbie 3 07-19-2013 11:45 AM
Grep on output of command returns all output traigo Linux - Newbie 2 02-24-2009 06:15 PM
Need Grep command for the following output minil Programming 2 12-29-2005 08:49 AM
Need Grep command for the following output minil Programming 1 12-29-2005 07:51 AM


All times are GMT -5. The time now is 12:43 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration