3

I have two scripts that open and close an app; let's say it's the builtin Calculator.app. The "start" script uses /usr/bin/open to launch the app, then a one-line AppleScript to set the app's visibility to false. This is the entirety of the "stop" script:

#!/bin/bash

osascript -e 'tell app "Calculator" to quit'

I am running these scripts from a LaunchDaemon that calls them at certain fixed times of the week.

The strange behavior I'm seeing is that sometimes, after the "stop" script runs, the app's Dock icon retains its little dot (suggesting it's still running). However:

  • right-clicking the Dock icon shows "Application not responding"
  • force-quitting via the Dock icon's menu does nothing
  • ps does not show any Calculator process running

And, most bafflingly, trying to launch Calculator again (via open, via Spotlight, or by double-clicking its icon) produces a modal dialog that says The application "Calculator" is not open anymore.

lsof shows these entries that might be related:

COMMAND     PID   USER   FD     TYPE             DEVICE   SIZE/OFF     NODE NAME
Spotlight   443 jteach   54r     DIR                1,4        102 28977573 /Applications/Calculator.app
Notificat   476 jteach  txt      REG                1,4      37933 29214948 /System/Library/Frameworks/NotificationCenter.framework/PlugIns/com.apple.ncplugin.calculator.appex/Contents/Resources/Assets.car
Notificat   476 jteach    8r     REG                1,4      37933 29214948 /System/Library/Frameworks/NotificationCenter.framework/PlugIns/com.apple.ncplugin.calculator.appex/Contents/Resources/Assets.car

My questions:

  • Why might my "stop" script be failing to make the app exit cleanly?
  • Short of rebooting, is there a way I can get the app out of this zombie state?
  • How can I alter my script to be sure that the app quits properly?
  • 1
    Since you’re using bash anyway, have you thought about using kill instead of using AppleScript ? – CJK Mar 1 '18 at 18:03
  • 2
    Thanks for the suggestion. I was thinking I'd try using kill or killall. I am nonetheless still interested in figuring out what's going on, and how to get the zombie app to go away. – RenaissanceBug Mar 1 '18 at 19:05
  • 1
    For the record, using killall seems to reliably not leave a zombie app. – RenaissanceBug Mar 8 '18 at 16:53
  • Correction: Nope, killall still leaves a zombie sometimes. So, my question still needs an answer. – RenaissanceBug Apr 6 '18 at 22:17
  • 1
    Update: This question has an answer that provides a working way to kill the zombies: sudo killall launchservicesd followed by sudo killall Dock. – RenaissanceBug Apr 14 '18 at 4:33
1

It seems like you have found a solution to your problem, but in case others are looking for more tools for their Unix-y toolbelt, here are a few:

quit

I have kept Jon Stovell’s shell script quit (direct download link) around for several years now, and it seems to do a very good job of quitting apps. It tries a variety of methods, and will try to allow an app to ”save changes” if necessary.

appswitch

Another very useful tool is Nicholas Riley’s appswitch (GitHub page) which will let you do a whole slew of things to apps, including quitting them (or hiding them, or showing their windows but not making them active, etc).

pkill

killall has already been mentioned, but I’d be remiss if I didn’t add that you could also use:

pkill -x Calculator

to get rid of the pesky app. I haven’t seen “dock zombies” so I can’t so whether or not that would fix the problem, but if it does, it might be preferable to two sudo commands.

A note about open

You mentioned that you are launching the app with open and then using AppleScript to hide it. Well-behaved apps should be respond to the -j flag to open to accomplish this without AppleScript, i.e.:

open -j -a Calculator

(see man open for details, such as "-j Launches the app hidden"; not to be confused with -g which means "Do not bring the application to the foreground." I can never remember which is which so I almost always use open -g -j -a Foo whenever I want to launch an app but not have it steal focus.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .