Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Using top command to email if process is exceeding 25% and sending an email alert if so

Tags
script for mailing line count, shell scripts, top

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 03-02-2012
Java Using top command to email if process is exceeding 25% and sending an email alert if so

This is my first time writing a script and Im having some trouble,
Im trying to use the top command to monitor processes and the amount of CPU usage they require, my aim is to get an email if a process takes over a certain percentage of CPU usage
I tried grep [25%-100%]
Obviosly that hasnt worked,
Any help would be much appreciated

FYI im using the bash shell for the script
# 2  
Old 03-02-2012
top's an interactive program and its output notoriously hard to interpret programatically. Why not just use ps, which outputs in a tabular format and probably includes similar information?

What's your system?

What's ps aux look like for you?
The Following User Says Thank You to Corona688 For This Useful Post:
jay02 (03-02-2012)
# 3  
Old 03-02-2012
Try something like

Code:
ps -eo pri,psr,pcpu,stat | awk '{if($3 >= 25)print $0}'

The Following User Says Thank You to codemaniac For This Useful Post:
jay02 (03-02-2012)
# 4  
Old 03-02-2012
when I input that command it comes back with this
Code:
awk: syntax error near line 1
awk: illegal statement near line 1
ps: unknown output format: -o stat
usage: ps [ -aAdeflcjLPyZ ] [ -o format ] [ -t termlist ]
        [ -u userlist ] [ -U userlist ] [ -G grouplist ]
        [ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ]
  'format' is one or more of:
        user ruser group rgroup uid ruid gid rgid pid ppid pgid sid taskid ctid
        pri opri pcpu pmem vsz rss osz nice class time etime stime zone zoneid
        f s c lwp nlwp psr tty addr wchan fname comm args projid project pset


Last edited by Scott; 03-02-2012 at 12:13 PM.. Reason: Code tags
# 5  
Old 03-02-2012
This is why I asked what your system is, and why I asked what your ps aux looks like. Without knowing that, we're bound to suggest things that could never work for you.

Though, I think you made a typo somewhere when typing in the awk statement. When typed in letter-for-letter, there's absolutely nothing wrong with it.
# 6  
Old 03-02-2012
Correctly suggested by Corona688 above , first you have to check in what environment you are in .Please check your ps's manual and grep out which options are available when you use -o switch .I guess the stat option is not natively supported in your environment , unlike mine .
# 7  
Old 03-02-2012
does this help:
Code:
bash-3.00# uname
SunOS
bash-3.00# ps aux
usage: ps [ -aAdeflcjLPyZ ] [ -o format ] [ -t termlist ]
        [ -u userlist ] [ -U userlist ] [ -G grouplist ]
        [ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ]
  'format' is one or more of:
        user ruser group rgroup uid ruid gid rgid pid ppid pgid sid taskid ctid
        pri opri pcpu pmem vsz rss osz nice class time etime stime zone zoneid
        f s c lwp nlwp psr tty addr wchan fname comm args projid project pset

---------- Post updated at 04:07 PM ---------- Previous update was at 03:43 PM ----------

Almost there thanks to both of you 2's help, one thing though instead of the 'print $0' im trying to substitute it to write to a file so I can then email that file to myself, is this possible?

Last edited by Scott; 03-02-2012 at 12:13 PM.. Reason: Code tags
The Following User Says Thank You to jay02 For This Useful Post:
Corona688 (03-02-2012)
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sending output of command via email ab52 Shell Programming and Scripting 4 02-24-2011 10:58 AM
sending an email RandomAccess Linux 1 03-05-2010 02:37 PM
sending email as background process satnamx Shell Programming and Scripting 3 08-18-2009 04:39 AM
email alerts for memory or cpu exceeding thresholds zxmaus AIX 4 05-22-2009 07:01 AM
sending an attachment through email Sgiri1 Shell Programming and Scripting 1 04-21-2009 12:46 AM
Sending email to a User from C++ debu Programming 1 01-23-2008 05:09 AM
Sending find command results to email gardellap Shell Programming and Scripting 3 04-27-2007 12:31 PM
sending email vasikaran UNIX for Dummies Questions & Answers 1 07-05-2005 05:50 AM
Sending email bcheaib UNIX for Dummies Questions & Answers 7 02-22-2005 08:03 AM


All times are GMT -4. The time now is 01:16 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password