LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-24-2006, 04:16 AM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
bash script error


I just wrote this script which will SSH to a host do a "ps" and put the result in the a file.txt & if the file size is 0 (which means "ps" didn't return anything) then it should send an email saying the service is down. But I am getting false positive & mail to lots of receipent like:

me@domain.com, service@domain.com, is@domain.com, not@domain.com, OK@domain.com

Code:
#!/bin/bash

USER="me"
SERVER="10.1.1.1"

SSH_KEY="my_key"

RECP="me@domain.com"

SUBJ1="Service check is NOT OK"

# ssh to the remote box & grep for the service
# & put the result in service.txt file

SSH_COMMAND_CHECK="`ssh -i $SSH_KEY $USER@$SERVER ps auxw | 
grep service | grep -v grep > service.txt`"

# get the size of the service.txt file

CHECK1="`ls -al service.txt | awk '{print $5}'`"

if [ "$CHECK1" != 0 ]; then
    echo "Service check is NOT OK" | mail -s $SUBJ1 $RECP
fi

Last edited by noir911; 07-24-2006 at 04:19 AM.
 
Old 07-24-2006, 04:42 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,148

Rep: Reputation: 236Reputation: 236Reputation: 236
Hi

You might want to quote the subject in the mail command:

if [ "$CHECK1" != 0 ]; then
echo "Service check is NOT OK" | mail -s "$SUBJ1" $RECP
fi

Otherwise only the first word of the subject becomes subject, and the rest will be email addresses.
 
Old 07-24-2006, 04:52 AM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
As for the false positives, try "#! /bin/bash -x" in the first line, it will tell you what's going on exactly.
 
Old 07-24-2006, 05:32 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
i don't have access to my console yet so this is not yet tested.
just try this mod anyway:

Code:
#!/bin/bash

USER="me"
SERVER="10.1.1.1"

SSH_KEY="my_key"

RECP="me@domain.com"

SUBJ1="Service check is NOT OK"

# ssh to the remote box & grep for the service
# & put the result in service.txt file

SSH_COMMAND_CHECK="$(ssh -i $SSH_KEY ${USER}@${SERVER} ps auxw | grep service | grep -v grep > service.txt)"

# get the size of the service.txt file

CHECK1="$(ls -al service.txt | awk '{print $5}')"

if [ $CHECK1 -ne 0 ]; then
    echo "Service check is NOT OK" | mail -s $SUBJ1 $RECP
fi
 
Old 07-25-2006, 03:58 AM   #5
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
Thanks. I made the changes and the problem has been fixed. Basically I changed

if [ $CHECK1 -ne 0 ]; then

to

if [ $CHECK1 -eq 0 ]; then

as I will be looking for a null file which means SSH didn't return anything.

I also changed

mail -s $SUBJ1 $RECP

to

mail -s "$SUBJ1" $RECP
 
  


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
error when tying to run python script(bash error?) shanenin Programming 5 01-10-2006 10:01 AM
bash script error near 'else' ? nutthick Linux - Newbie 5 01-23-2005 03:41 PM
bash script error corbis_demon Linux - Newbie 7 09-27-2004 10:41 AM
bash script problem...or user error? Jalalabee Programming 2 09-10-2003 06:18 AM
BASH script error in RH 7.3 ..whaaaa??? Pauly Linux - Software 7 12-13-2002 09:47 AM


All times are GMT -5. The time now is 06:08 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration