Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript : How to get the PID with highest CPU usage and kill it

User uploaded file


Please refer to my attached image, recently I'm trying to write a appleScript to get the PID with highest CPU consume, and then terminate it.

I have try to search for solution from google, but didn't found any proper solution on this problem.

appreciate that anyone can help me on this problem ^_^

any solution would be welcome....

MacBook Air, iOS 8.0.2

Posted on Oct 18, 2014 10:52 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 19, 2014 6:41 AM

Your problem statement is a bit too simple. If you just pick the highest accumulated CPU usage, you can easily choose an essential system process and end up wedging your system.


If you are having performance issues, it would be better to describe your problem, along with which version of Mac OS X you are running. Maybe even post the EtreCheck output for analysis

<https://discussions.apple.com/docs/DOC-6174>


pid=$(ps ax -o cputime,pid|sort -n|tail -1)

kill ${pid#*\ }


wrap that in an Applescript and it will delete the process with the most accumulated CPU time. Not sure it is a good idea, but it is your Mac to do with as you please.


See:

man ps

man sort

man tail

man bash # Parameter Expansion and Command Substitution

6 replies
Sort By: 
Question marked as Top-ranking reply

Oct 19, 2014 6:41 AM in response to darkandy

Your problem statement is a bit too simple. If you just pick the highest accumulated CPU usage, you can easily choose an essential system process and end up wedging your system.


If you are having performance issues, it would be better to describe your problem, along with which version of Mac OS X you are running. Maybe even post the EtreCheck output for analysis

<https://discussions.apple.com/docs/DOC-6174>


pid=$(ps ax -o cputime,pid|sort -n|tail -1)

kill ${pid#*\ }


wrap that in an Applescript and it will delete the process with the most accumulated CPU time. Not sure it is a good idea, but it is your Mac to do with as you please.


See:

man ps

man sort

man tail

man bash # Parameter Expansion and Command Substitution

Reply

Oct 21, 2014 6:02 AM in response to darkandy

The first time I looked at your post, the forums would not display your screen shot, so I did not see you were trying to kill the highest CPU percentage for an httpd daemon (aka Apache web server). The command string I would use for that would be:


pid=$(ps ax -o %cpu,comm|grep httpd|sort -n|tail -1)

kill ${pid#*\ }


This changed cuptime (which is accumulated CPU) to %cpu which appears to be what you are interested in. NOTE: %cpu is only an instant in time value, so unless the httpd daemon is wedged running some server side scripting, it may be a random selection.


Also added is a 'grep' command to select just from the 'httpd' processes. This is a lot less random and at worse will only mess up your Apache web server.

Reply

Oct 21, 2014 8:43 AM in response to BobHarris

Bob,


Thoughts about the following?


Using grep [Hh]ttpd would eliminate the need for an extra pipe into tail -1. The nature of apachectl start is that it requires sudo, starts two htppd daemons, and requires sudo kill pid(s) to kill the processes. Well, actually restart them. Your command example for my two running httpd processes returns only one instance that appears as below without a pid to assign to the shell variable and subsequently kill.


0.0 /usr/sbin/httpd


$(ps -ax -o pid,%cpu,comm | grep [Hh]ttpd | sort -n) returns:


1293 0.0 /usr/sbin/httpd

1294 0.0 /usr/sbin/httpd


and when assigned to a pid shell variable, contains:


12930.0/usr/sbin/httpd12940.0/usr/sbin/httpd


which can be handled by piping the above syntax into:


pid=$(ps -ax -o pid,%cpu,comm | grep [Hh]ttpd | sort -n | grep -oE "(\d{2,4})")


1293 1294


and sudo kill $pid then works.

Reply

Oct 21, 2014 8:50 AM in response to VikingOSX

VikingOSX

Yes, The grep '[h]ttpd' trick is something I figured out years ago, but I was just "Sloppy" with my previous post. "My Bad"

However, darkandy only wanted the highest CPU, not all 'httpd' daemons. Which is why I had the tail -1 in the pipeline.

Except for that, your approach, expecially the '[h]ttpd' regular expression to keep grep from finding itself in the ps list is good.

Reply

AppleScript : How to get the PID with highest CPU usage and kill it

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.