Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Tags
script for mailing line count, top

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-02-2012   -   Original Discussion by jay02
jay02's Unix or Linux Image
jay02 jay02 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 9 March 2012, 7:09 AM EST
Posts: 8
Thanks: 4
Thanked 1 Time in 1 Post
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 Linux

FYI im using the bash shell for the script
Sponsored Links
    #2  
Old Unix and Linux 03-02-2012   -   Original Discussion by jay02
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 11 December 2017, 5:38 PM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
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)
Sponsored Links
    #3  
Old Unix and Linux 03-02-2012   -   Original Discussion by jay02
codemaniac's Unix or Linux Image
codemaniac codemaniac is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 15 August 2016, 4:00 PM EDT
Location: /home/arijit
Posts: 120
Thanks: 3
Thanked 24 Times in 24 Posts
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 Unix and Linux 03-02-2012   -   Original Discussion by jay02
jay02's Unix or Linux Image
jay02 jay02 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 9 March 2012, 7:09 AM EST
Posts: 8
Thanks: 4
Thanked 1 Time in 1 Post
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 01:13 PM.. Reason: Code tags
Sponsored Links
    #5  
Old Unix and Linux 03-02-2012   -   Original Discussion by jay02
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 11 December 2017, 5:38 PM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
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.
Sponsored Links
    #6  
Old Unix and Linux 03-02-2012   -   Original Discussion by jay02
codemaniac's Unix or Linux Image
codemaniac codemaniac is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 15 August 2016, 4:00 PM EDT
Location: /home/arijit
Posts: 120
Thanks: 3
Thanked 24 Times in 24 Posts
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 .
Sponsored Links
    #7  
Old Unix and Linux 03-02-2012   -   Original Discussion by jay02
jay02's Unix or Linux Image
jay02 jay02 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 9 March 2012, 7:09 AM EST
Posts: 8
Thanks: 4
Thanked 1 Time in 1 Post
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 01:13 PM.. Reason: Code tags
The Following User Says Thank You to jay02 For This Useful Post:
Corona688 (03-02-2012)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:58 AM
sending email as background process satnamx Shell Programming and Scripting 3 08-18-2009 05:39 AM
email alerts for memory or cpu exceeding thresholds zxmaus AIX 4 05-22-2009 08:01 AM
Sending find command results to email gardellap Shell Programming and Scripting 3 04-27-2007 01:31 PM



All times are GMT -4. The time now is 11:47 AM.