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

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

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.

7

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.

This would list and kill all the sessions:

tmux list-sessions | awk 'BEGIN{FS=":"}{print $1}' | \ ifne xargs -n 1 tmux kill-session -t
2

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. How to kill all tmux sessions (or at least multiple sessions) from the CLI?

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.

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

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.

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like