Skip navigation

Checking for process and closing if it exists?

415 Views 2 Replies Latest reply: Feb 4, 2013 10:45 AM by twtwtw RSS
MacMan240 Level 1 Level 1 (80 points)
Currently Being Moderated
Feb 4, 2013 8:36 AM

A current part of my code is:

 

property thePID : missing value

 

on disableSleep_(sender)

        set thePID to (do shell script "caffeinate &> /dev/null & echo $!")

    end disableSleep_

    on enableSleep_(sender)

        if thePID is missing value then return -- no process

        do shell script "kill $(pgrep caffeinate) " & thePID

        set thePID to missing value -- reset for next time

    end enableSleep_

 

At the end of the program I have:

 

on applicationShouldTerminateAfterLastWindowClosed_(sender)

        return true

    end applicationShouldTerminateAfterLastWindowClosed_

          on applicationShouldTerminate_(sender)

                    return current application's NSTerminateNow

          end applicationShouldTerminate_

 

And I would like it to check to see if the process "caffeinate" exists, and if it does, I would like it to kill all "caffeinate" processes. Any ideas?

MacBook Pro, OS X Mountain Lion (10.8.2), 2.3 GHz i5, 4GB RAM, OCZ 120GB SSD
  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 4, 2013 9:01 AM (in response to MacMan240)

    Using killall with the process name will kill all of those processes - the shell script will just return an error message if the process name doesn't exist:

     

    do shell script "killall caffeinate"

     

    for example (tested):

     

        on disableSleep_(sender)

            if thePID is missing value then -- not running yet

              set thePID to (do shell script "caffeinate &> /dev/null & echo $!")

            end if

        end disableSleep_

     

        on enableSleep_(sender)

            try -- skip error if process name doesn't exist

              do shell script "killall caffeinate"

            end try

            set thePID to missing value -- reset for next time

        end enableSleep_

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Feb 4, 2013 10:45 AM (in response to MacMan240)

    a slightly different (and somewhat easier) approach to the problem.  Use a launchd plist that you runs caffeinate, then load and unload that plist as needed.  save the following property list as a plain-text file at ~/Library/LaunchAgents/user.nodoze.plist":

     

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

    <plist version="1.0">

    <dict>

              <key>Disabled</key>

              <true/>

              <key>RunAtLoad</key>

              <true/>

              <key>Label</key>

              <string>user.nodoze</string>

              <key>Program</key>

              <string>/usr/bin/caffeinate</string>

    </dict>

    </plist>

     

    Then run the following applescript to start and stop it:

     

    if (do shell script "launchctl list") contains "user.nodoze" then

              set r to "enabled."

    else

              set r to "disabled."

    end if

     

    set b to button returned of (display alert "Caffeinate is currently " & r buttons {"Never Mind", "Enable", "Disable"})

     

    if b is "Enable" then

              do shell script "launchctl load -F ~/Library/LaunchAgents/user.nodoze.plist"

    else if b is "Disable" then

              do shell script "launchctl unload ~/Library/LaunchAgents/user.nodoze.plist"

    end if

     

    the Disabled key is set to true to keep the plist from loading when you log in; the -F option overrides the Disabled key to load the job on demand.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.