LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-03-2011, 08:04 AM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 88

Rep: Reputation: 15
Question Sending an e-mail if lsof count exceeds a certain value.


I am trying to write a cronjob which runs every 5 minutes so that when the count exceeds a certain number, I get an e-mail.

So far I have managed to write the cronjob so that it sends an e-mail with the count of lsof.

*/5 * * * * /usr/sbin/lsof -p 7642 2>&1 > /root/lsof_output.txt;less lsof_output.txt | wc -l > lsof_count.txt; /bin/mailx -s "lsof count for process" <e-mail id> < /root/lsof_count.txt

However, I am not sure how to add an if statement and run the mail command only if the count exceeds 1000.

Any suggestions/ideas are greatly appreciated.

Thanks,

Last edited by threezerous; 10-03-2011 at 09:43 AM.
 
Old 10-03-2011, 09:23 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Write a script with your commands in and then get cron to run the script, e.g.

Code:
#!/bin/bash

lines=$(cat file | wc -l | awk '{print $1}')

if [ "$lines" -gt 10 ]
then
  echo "There are more than 10 lines in the file!"
fi
Suppose the script was called "linecount.sh", I'd make it executable with

Code:
chmod +x linecount.sh
and then in my crontab, I'd give the full path to it (e.g. if it resided in my home directory, I'd use /home/me/linecount.sh).

You might want to look at the Advanced Bash-Scripting Guide.

Also, I'd remove your email address from your post, as you'll just open yourself up to being spammed.
 
  


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
Cannot mount hard disk: block count exceeds size of device --- bad partition table R00ts Linux - General 15 06-01-2014 03:58 PM
Sendmail (mailserver) -- sending mail with the mail command -- doesn't work :-( carolinevdh Linux - Server 1 08-13-2009 09:43 PM
Mailserver sending but not sending mail from outside. simango Linux - Newbie 1 10-28-2008 08:51 AM
Sending ICMP messages of type 3 when packet size exceeds MTU of the interface nmb1081 Linux - Networking 1 04-13-2008 10:14 PM
Postfix mail transfer agent not sending mail locally or to other servers sketelsen Linux - Software 3 02-09-2006 11:16 AM


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