How to force quit Applescript app from terminal

applescript

I wrote an applescript that turns on my lights. I exported it as an application with the name "LightsOn", and it appears with that name in activity monitor.

LightsOn in Activity Monitor

Under certain circumstances, this app gets stuck. I can force quit it from Activity Monitor, but I want to be able to do so from a script. killall LightsOn doesn't work; I'm told that no matching processes belonging to you were found.

How can properly force quit this app from the terminal? Or, how can I discover the appropriate name for killall?

Best Answer

The reason killall LightsOn doesn't work is because all running AppleScript application's process name is applet. For example, the executable path is:

../LightsOn.app/Contents/MacOS/applet

In general and assuming the process(es) is(are) not hung, one could use killall applet however that will terminate all running AppleScript applications, and of course that may not be desirable.

When a process is hung, you'll need to use a KILL signal, e.g.:

killall -KILL applet

However, pkill is the way to go to easily target a specific AppleScript application and because it's hung... use a KILL signal, e.g.:

pkill -9 -f LightsOn

Or:

pkill -KILL -f LightsOn

Note in this instance, -9 is just another way of saying -KILL.

Now with pkill and using the -f option, one can use more of the full argument list, e.g.:

pkill -9 -f /Applications/LightsOn.app/Contents/MacOS/applet

Then there's no ambiguity as to which applet process one is targeting. One can use as much of the full argument list as one feels is necessary to target the correct process.

For example, pkill -9 -f LightsOn will terminate an AppleScript application named NoLightsOn, so always use enough of the full argument list to terminate the correct process.


For reference:

From the pkill man page:

 -f          Match against full argument lists.  The default is to match
             against process names.

From the killall man page:

 Some of the more commonly used signals:

 1       HUP (hang up)
 2       INT (interrupt)
 3       QUIT (quit)
 6       ABRT (abort)
 9       KILL (non-catchable, non-ignorable kill)
 14      ALRM (alarm clock)
 15      TERM (software termination signal)

Note that this is just a partial signal list and is used with many of the different utilities that can terminate a process. See the various manual pages of the utilities used for additional information.