Go Back   MacRumors Forums > Apple Systems and Services > Mac Basics and Help

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 25, 2012, 05:39 PM   #1
UFGrayMatter
macrumors newbie
 
Join Date: Oct 2012
Script to auto quit hung app

I'm coming home or waking up to a computer that is running 99% CPU because of a hung app. The app developers know of the issue and are working on it.

Is there a way/script that searches for apps that are hung and auto quits them?

Thanks
UFGrayMatter is offline   0 Reply With Quote
Old Oct 25, 2012, 09:24 PM   #2
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Try this :

Code:
-- Application VLC as example. Replace with the hung application
on idle
	tell application "System Events"
		set PID to unix id of process "VLC"
	end tell
	set cpuUsage to (do shell script "ps -c -o %cpu='' -p " & PID) as number
	if cpuUsage is greater than "52.0" then
		tell application "VLC" to quit
		tell me to quit
	end if
	-- handler will be called every 10 seconds. Adjust to your need
	return 10
end idle
Save the script as a stay-open application.
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Oct 25, 2012, 09:34 PM   #3
UFGrayMatter
Thread Starter
macrumors newbie
 
Join Date: Oct 2012
Quote:
Originally Posted by kryten2 View Post
Try this :

Code:
-- Application VLC as example. Replace with the hung application
on idle
	tell application "System Events"
		set PID to unix id of process "VLC"
	end tell
	set cpuUsage to (do shell script "ps -c -o %cpu='' -p " & PID) as number
	if cpuUsage is greater than "52.0" then
		tell application "VLC" to quit
		tell me to quit
	end if
	-- handler will be called every 10 seconds. Adjust to your need
	return 10
end idle
Save the script as a stay-open application.
Thank you for that - I'll give it a try.

One question - the process is a Python process - and there are two of them running at the same time. One Python process is normal, the other is the one that hangs. I currently manually quit the hung process and then restart it without the other having any issue. How would this script work for two processes being named "Python?"
UFGrayMatter is offline   0 Reply With Quote
Old Oct 25, 2012, 09:56 PM   #4
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
I thought it was an application. How do you manually quit the hung process? What's the name of this app? Can you give a link for some information? Lookup the PID with Activity Monitor or top is all I can suggest.
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Oct 26, 2012, 06:32 AM   #5
UFGrayMatter
Thread Starter
macrumors newbie
 
Join Date: Oct 2012
attached is a screenshot of of the activity monitor - the current processes are running as they should, there are none hanging.

Both Python processes are running two different python scripts - one is sickbeard and one is couchpotato.
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2012-10-26 at 6.27.54 AM.png
Views:	18
Size:	22.4 KB
ID:	372382  
UFGrayMatter is offline   0 Reply With Quote
Old Oct 26, 2012, 06:34 PM   #6
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
If sickbeard is the one likely to hang you can try this :

In Terminal :

Code:
python SickBeard.py & echo $! > /tmp/SickBeard.pid
with script :

Code:
on idle
	set PID to do shell script "cat /tmp/SickBeard.pid"
	set cpuUsage to (do shell script "ps -c -o %cpu='' -p " & PID) as number
	if cpuUsage is greater than "70.0" then
		do shell script "kill -9 " & PID
		tell me to quit
	end if
	return 10
end idle
Save the script as a stay-open application.

CouchPotato is available as a native OSX application.
Attached Thumbnails
Click image for larger version

Name:	Screen shot 2012-10-27 at 00.27.23.png
Views:	16
Size:	66.9 KB
ID:	372503  
Attached Images
 
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Nov 3, 2012, 01:09 PM   #7
UFGrayMatter
Thread Starter
macrumors newbie
 
Join Date: Oct 2012
kryten2,

This works great. Thanks for the heads up with CouchPotato, I have that running now too.

Right now, if the python script hangs, sickbeard.py quits, but python stays open. Ideally I'd like to have the script that checks if sickbeard is hung quit python and then restart it as well.

Do you think that is possible?

Thanks!
UFGrayMatter is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Mac Basics and Help

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Can you write a script to auto delete text messages? bobfitz14 iOS 7 6 Mar 23, 2014 05:26 PM
Want to auto quit inactive application icposse2k OS X 1 Feb 4, 2014 02:20 PM
Auto-Quit Program with Automator ReDDMax OS X 0 Nov 17, 2013 07:18 PM
My script is making it impossible to quit an app it works with Mephistopelus Mac Programming 9 Oct 17, 2012 09:39 AM
Applications Hung up and Force Quit trlyka MacBook Pro 3 Sep 12, 2012 11:30 PM

Forum Jump

All times are GMT -5. The time now is 07:27 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC