16

I noticed that i have a running process and I think it's because i initialized the process in byobu which is using tmux.

ps aux | grep sidekiq
1000     13786  0.0  0.0   8108   900 pts/4    S+   11:27   0:00 grep sidekiq

I have a tmux session called "3". As you can see below:

$ byobu   

Byobu sessions...

  1. tmux: 3: 1 windows (created Wed Aug 28 10:57:54 2013) [229x84] (attached)
  2. tmux: daemon: 1 windows (created Thu Jul 11 12:59:09 2013) [127x83]
  3. tmux: juggernaut: 1 windows (created Thu Jul 11 12:54:08 2013) [80x23]

I would like to exit that session and end all running processes that were created by that session.

JohnMerlino
  • 5,941
  • 11
  • 34
  • 38

4 Answers4

18
tmux kill-session [-t session_name]

the processes in the virtual terminals should receive SIGHUP.

peterph
  • 30,520
  • 2
  • 69
  • 75
  • 11
    but SIGHUP doesn't kill processes. it usually *respanws* them. – Andrea Pavoni Jun 04 '14 at 12:39
  • 1
    Depends on what processes you are talking about. Some daemons indeed do use SIGHUP as a signal to reinitialize (re-read the configuration files etc.), but these usually fork away from the process group and the controlling terminal and have to be considered independent from then on. Interactive applications usually quit either because of not handling the signal or because losing terminal kind of goes against being interactive. – peterph Jun 05 '14 at 14:34
7

This was bothering me as well, so I wrote a tmux plugin for this. It's not perfect, but can easily be extended to know how to safely kill most kinds of processes before exiting tmux: tmux-safekill

Jonathan Lipps
  • 171
  • 1
  • 2
6

Maybe it's not the most elegant solution, but does what you asked for:

tmux list-panes -s -F "#{pane_pid} #{pane_current_command}" | grep -v tmux | awk '{print $1}' | xargs kill -9

If you run this from within your tmux-session, it will kill all the processes it spawned, and you can then quit tmux with exit.

Hubro
  • 1,030
  • 1
  • 11
  • 19
1

(disclaimer: i am beginner) This script terminates all panes/windows in some_session and exits:

#!/usr/bin/env bash

session="some_name"

echo about to kill ${session}, ok?
read -n 1 -s -r -p "Press key..."
echo 

sessiontest=`tmux ls | grep ${session}`

if [ "${sessiontest}" == "" ]; 
then
        echo no running session ${session}
else        
        for name in `tmux list-windows -F '#{window_name}' -t ${session}` ; do 

        tmux select-window -n

        for pane in `tmux list-panes -F '#{pane_id}' -t ${session}` ; do 
        tmux send-keys -t $pane C-c
        # send SIGINT to all panes in selected window
        echo ${session}:$name.${pane//%}
        done

        for pane in `tmux list-panes -F '#{pane_pid}' -t ${session}` ; do 
        kill -TERM ${pane}
        # terminate pane
        done

        done 
fi

echo list-sessions:
tmux list-sessions
droid192
  • 138
  • 4