LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 01-03-2008, 09:15 AM   #1
sir-lancealot
Member
 
Registered: Aug 2007
Posts: 346

Rep: Reputation: 31
bash noob question


ok, I am getting dangerous now, but we have a problem with an apache memory leak and it's taking out the box's. So for now I am babysitting them using a free -m across the network, getting the swap in use, if it's over 300 or so, restarting apache (long story). While the developers track down, I would like to set this up as a script to check, if over 300 send an email, so this;
free -m | awk '/Swap/ {print $3}'
gives me exactly what I want, so now I need to say in a script;

if free -m | awk '/Swap/ {print $3}' > 300
{
email me
}
, then stick that in a cron. I know php, cf, etc. so this will take the right person/people 2 min's to reply vs. me starting at page 1, hello world.

Tnx
 
Old 01-03-2008, 10:14 AM   #2
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
Code:
#!/bin/bash
MEM_VAL=`free -m | awk '/Swap/ {print $3}'`
if [ ${MEM_VAL} -gt 300 ]; then
echo MEM_VAL=$MEM_VAL | mail -s "Alert Mem Limit" person@mail.com
fi
exit
Something like that might be enough

Last edited by helptonewbie; 01-03-2008 at 11:11 AM.
 
Old 01-03-2008, 10:34 AM   #3
sir-lancealot
Member
 
Registered: Aug 2007
Posts: 346

Original Poster
Rep: Reputation: 31
thanks, I put that in my home, made it executable, and ran it;

./memchk.sh <enter> and it just sat there. I put it on a box that had a swap in use of around 300 (so it would trip it) and it just sat there. A <ctrl>-C showed, (Interrupt -- one more to kill letter)
but it never sent the email.

I also upped the limit to 500 so it wouldn't trip, ran and it went through back to command line, so it must be something missing like a ', ; etc.

But thanks for the quick reply, just need to kick it one more time!
 
Old 01-03-2008, 10:53 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
# free -m | (awk '/Swap/ && $4 > 300' && mail -s "Alert new" root)
 
Old 01-03-2008, 10:58 AM   #5
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
my bad i edited the above post try that
 
Old 01-03-2008, 11:05 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by helptonewbie View Post
Code:
#!/bin/bash
MEM_VAL=`free -m | awk '/Swap/ {print $3}'`
if [ ${MEM_VAL} -gt 300 ]; then
echo MEM_VAL=$MEM_VAL > /tmp/tmpfile
mail -s "Alert Mem Limit" person@mail.com < tempfile
fi
exit
Something like that might be enough
you can just do this. No need to create tempfile. ( btw, the tempfile and tmpfile, different spelling and path.)
Code:
echo MEM_VAL=$MEM_VAL | mail -s "Alert Mem Limit" person@mail.com
 
Old 01-03-2008, 11:09 AM   #7
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
ah yea that would work, dumb diddy um dumb dumb :-)
I'll re-edit
 
Old 01-03-2008, 12:17 PM   #8
sir-lancealot
Member
 
Registered: Aug 2007
Posts: 346

Original Poster
Rep: Reputation: 31
lol, worked like a charm, thanks both.

Now I can start working on some dangerous bash scripts!

Happy New Year !!!!

:lr:
 
Old 01-03-2008, 01:56 PM   #9
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
Just for future reference, it wasn't working because the first mail command i gave you didn't contain any text in the main body of the email, i thought that an email with just a subject name should send but it appears not to, hense why i then went and tried a input re-direct into the mail which does work but yes, using a pipe as the input also works so either way would have worked the '|' method is much neater :-)
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
BSD noob [adding bash] simplebob *BSD 5 04-03-2006 02:27 AM
noob bash script question babag Programming 9 04-30-2005 02:25 AM
stuck again (noob bash math question) babag Programming 6 04-25-2005 01:27 AM
noob with a noob question phoonerorlater Linux - Newbie 2 09-29-2004 03:43 PM
Complete noob question from a noob noob_hampster Linux - Software 2 09-04-2003 12:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:47 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