56

I want to quit certain applications on my Mac using Terminal. For example, how do I kill “Slack”? Do I need its PID number?

Udhy
  • 6,697
Brainmaniac
  • 1,577
  • 3
  • 14
  • 15

9 Answers9

77

You can use AppleScript to tell the application to quit:

osascript -e 'quit app "Slack"'

this will tell the application to quit and will start all the save and cleanup tasks. Or you can send the TERM signal with pkill but it could be that the application will not shut down cleanly

pkill -x Slack
Matteo
  • 8,855
  • 12
    This is the best method, because it replicates what happens when you use File>Quit from the menu. – Barmar Mar 28 '19 at 16:15
  • 5
    Huh. I've always used osascript -e 'tell application "Slack" to quit' but if the shorter syntax works, then that's obviously preferable. This is definitely a better answer than anything suggesting kill or any of its variants. – TJ Luoma Mar 30 '19 at 02:58
  • @TJLuoma I think if you select the correct signal, then pkill is as graceful as any other method. I too favour the AppleScript, though created a function quit to make it breezy. However, if creating an AppleScript process isn’t necessary, pkill -QUIT -x Slack should be fine. – CJK Apr 17 '19 at 20:50
  • @CJK No signals are sent to a different procedure in the app than putting something on the message queue. If the app is well written then the signal should shut the app down properly but I suspect most apps aren't well written – mmmmmm May 20 '21 at 18:03
  • 2
    @mmmmmm You need a comma after "No", because its omission completely inverts your statement, which I only just realised after composing a reply to your comment as written to contest your assertion. Luckily, for some reason, I switched voice when reading it again in my head, and now I completely agree with what you said. But I'm not sure how it follows on from my comment 2 years ago (although I realise now that the default SIGTERM signal is supposed to terminate apps gracefully and permit clean-up tasks to be completed). – CJK May 23 '21 at 12:44
  • Yes it should have had a comma. The issue is pkill is not as graceful as the osascript in practice. Apps should deal with SIGTERM but it is not the main message loop and many don't. Also the osascript method allows the app to ask the user if they are sure - wether that is good or bad depends. – mmmmmm May 23 '21 at 12:47
  • I've tried both methods and only osascript works (AltTab in this case). If someone wants to add this in zsh, for example, you can try alias quit_alt_tab='osascript -e "quit app \"AltTab\""' – Anh-Thi DINH Feb 23 '22 at 18:24
57

No, you do not need to know its PID.

You can use:

pkill -x Slack

Or:

killall Slack

Note: Be sure to read the manual page for whichever command you choose to use, in order to see the various options available to the command, as may be relevant to its particular usage. In Terminal type e.g. man pkill and press enter, or just type the command and right-click on it, then select: Open man Page

user3439894
  • 58,676
17

Since I don't yet have the reputation to comment, I'm saying this as a separate answer. pkill without any flags does not match a specific process! For example, running pkill foo would target processes named foo, but would also target processes named foobar. This is because it uses regular expressions.

If you wish to kill a specific process, you can pass it the -x flag. For example, pkill -x foo. This will use exact names instead of regular expressions.

For example, in your case, pkill -x Slack will do the trick.

8

You can install htop (via brew for instance).

You'll need to run this as root or with sudo. Essentially, it's a text based Activity Monitor.

Select the process you want to kill (either with arrow keys or a mouse).

Then press k to send the process a signal and then 9 to choose the SIGKILL signal.

Dancrumb
  • 155
BEFio
  • 81
6

I'm not sure for Slack, but some Applications will run multiple Processes and you may want to kill just one (I find I often need to do this with iTunes, for example). In that case, you can run

ps -e | grep -i slack

To find all Processes with case-insensitive "slack" in the name. The output should look like (without the header):

PID TTY          TIME CMD
649 pts/1    00:00:00 bash

That first column will be your PID. You can then use that to kill specifically that process:

kill -9 649

Replacing 649 with your PID you found from calling ps.

scohe001
  • 609
  • 3
    kill sends a signal to terminate the app. It is not necessary to use -9 (kill) if the application is responding. You risk to loose unsaved data – Matteo Mar 28 '19 at 17:58
  • Similarly, if you did happen to want to kill, say, all 5 instances of some named process, you could filter the output of ps on the CMD value and then kill all the first entries on those lines. – Carl Witthoft Mar 29 '19 at 14:59
  • 1
    @Carl Witthoft, you can simply use killall procname, e.g. killall Slack and it will kill all occurrences of Slack or whatever procname is. No need to use ps! – user3439894 Mar 29 '19 at 22:26
  • So, pgrep is a nice command that combines ps and grip. – Harv Apr 03 '19 at 00:47
4

I added this to my .zshrc to quit an app using Matteo's answer

qapp() {
    osascript -e "quit app \"$1\""
}

Usage: $ qapp Slack

jav_solo
  • 173
2

If the application has a log name, make sure to give the full name which is listed in applications

e.g. close and then reopen

osascript -e 'quit app "Cisco AnyConnect Secure Mobility Client.app"'
open /Applications/Cisco/Cisco\ AnyConnect\ Secure\ Mobility\ Client.app/
nohillside
  • 100,768
1

If you are looking to kill Slack automatically at 6pm. Just add the following to your crontab

$ crontab -e

0 18 * * * pkill -x Slack
nohillside
  • 100,768
0

No, you can use pkill to terminate a running application. For example -

pkill Notes

If this fails, there is an alternate way

Get the process ID of the app using pgrep. For example -

pgrep Notes

Then to quit the app use

kill <process ID>
  • If pkill fails, then why would kill work, given that they do the same thing, i.e. send termination signals to a process ? – CJK May 23 '21 at 12:47