185

How to kill all tmux sessions (or at least multiple sessions) from the (Ubuntu) CLI?

When I do ps aux | grep tmux I see 3 processes:

UU      2970  0.0  0.0  19556  1228 pts/0    S+   02:48   0:00 tmux
UU      3445  0.0  0.0  12944   988 pts/5    S+   03:31   0:00 grep --color=auto tmux
UU     27557  0.0  0.2  29788  4840 ?        Ss   Jan04   0:02 tmux

How could I kill all of these at once (or at least some of them, selectivity) ?

7 Answers 7

312

You can use tmux kill-server to cleanly and gracefully kill all tmux open sessions (and server).

If you are inside a tmux session you would like to keep, use tmux kill-session -a to close all other sessions.

To close a specific session, use tmux list-sessions to identify the session you want to kill, and then use tmux kill-session -t targetSession to kill that specific session.

Also you can grossly kill all tmux processes with pkill -f tmux.

Hope it helps.

10
  • 2
    ps aux | grep tmux after tmux kill-server still shows a tmux process... Same goes with pkill -f tmux as in my answer. Can you explain this? Thanks,
    – user423047
    Jan 5, 2017 at 6:20
  • 2
    @Benia are you sure you are not just seeing the grep tmux process (which is included in the ps output of your original question)? Jan 5, 2017 at 13:45
  • 6
    If you issue ps aux | grep blablablawhichdoesntexist it will always show at least one line, corresponding to the grep blablablawhichdoesntexist command.
    – dgonzalez
    Jan 5, 2017 at 14:14
  • 1
    Are you missing -t flag, cos it shows error without -t flag set for killing a particular session. Usage: kill-session [-a] [-t target-session] Nov 1, 2017 at 3:03
  • 1
    It used to be very painful tmux ls | grep : | cut -d. -f1 | awk '{print substr($1, 0, length($1)-1)}' | xargs kill
    – Foad
    Feb 1, 2018 at 14:35
28

I can kill all of these processes with the command:

pkill -f tmux

It kills all processes (full list) of the matching name (tmux).


Note for newcomers: This way could serve you to kill all process of other matching names.

1
26

This would list and kill all the sessions:

tmux list-sessions | awk 'BEGIN{FS=":"}{print $1}' | \
     ifne xargs -n 1 tmux kill-session -t
3
  • to others who might run into this (like me) - if you're on a tmux session running this, you might want to kill all the other sessions first before killing your own :P
    – Ho Man
    Aug 23, 2021 at 19:39
  • @HoMan You might be interested on my answer Dec 2, 2021 at 4:13
  • what about by username? Nov 14, 2022 at 20:14
6

Given:

# tmux ls
session-0a: 1 windows (created Sat Dec  5 02:31:35 2020) [117x30]
session-84: 1 windows (created Sat Dec  5 01:55:18 2020) [190x47] (attached)
session-b3: 1 windows (created Sat Dec  5 03:23:44 2020) [94x13]
session-b2: 1 windows (created Sat Dec  5 02:45:00 2020) [104x14]
session-ae: 1 windows (created Sat Dec  5 01:55:18 2020) [190x47] (attached)

This will kill all sessions not attached by someone:

tmux list-sessions | grep -v attached | awk 'BEGIN{FS=":"}{print $1}' | xargs -n 1 tmux kill-session -t || echo No sessions to kill

References:

  1. https://stackoverflow.com/questions/10411616/grep-regex-not-containing-string
  2. https://unix.stackexchange.com/questions/33049/how-to-check-if-a-pipe-is-empty-and-run-a-command-on-the-data-if-it-isnt
  3. How to kill all tmux sessions (or at least multiple sessions) from the CLI?
2

Here another solution that allows you to select easily between useful sessions and the ones to delete, enter in tmux:

Check your sessions pressing:

ctrl+b+s

Then move with the arrows and press t to target as many sessions as you want to kill.

Press : and write

kill-session

I think this solution is the faster one as you don't need to write the name or number of the sessions to delete, but you rather use the UI of the own tmux to delete all selected at the same time.

In the same menu, you can also delete them one by one pressing x to delete and y to accept the deletion, so you write even less if you don't have many sessions to delete.

1

I have written a small app in C which does this.

It's called kkill, you can simply

kkill tmux tmux-server tmux-something etc

this will kill all tmux processes running. it is a lot simpler than pkill.

https://github.com/giorgossaridakis/kkill

21 December 2021 - updated kkill.c, added a tsr kkiller.c who 'll watch for processes and send them SIGKILL as they become active

4
0

Kill all sessions with no one attached (based on this answer):

tmux ls | awk 'BEGIN{FS=":"}!/attached/{print $1}' | xargs -n 1 tmux kill-ses -t

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy