Script to auto quit hung app

Discussion in 'Mac Basics and Help' started by UFGrayMatter, Oct 25, 2012.

  1. macrumors newbie

    Joined:
    Oct 25, 2012
    #1
    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
     
  2. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    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.
     
  3. thread starter macrumors newbie

    Joined:
    Oct 25, 2012
    #3
    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?"
     
  4. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    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.
     
  5. thread starter macrumors newbie

    Joined:
    Oct 25, 2012
    #5
    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 Files:

  6. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    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 Files:

  7. thread starter macrumors newbie

    Joined:
    Oct 25, 2012
    #7
    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!
     

Share This Page