Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-03-2011, 09:04 AM   #1
Registered: Jul 2009
Posts: 96

Rep: Reputation: 16
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.


Last edited by threezerous; 10-03-2011 at 10:43 AM.
Old 10-03-2011, 10:23 AM   #2
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.


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

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

chmod +x
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/

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.


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

All times are GMT -5. The time now is 05:27 AM.

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