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 :slight_smile:

FYI im using the bash shell for the script

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?

1 Like

Try something like

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

when I input that command it comes back with this

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

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.

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 .

does this help:

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?

1 Like

You can do a redirect to a file if you wish .Something like below .

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

The report will be appended to the file processUsage.log .

Thankyou your help is much appreciated! :slight_smile: