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.

1
  • Can you paste the output of 'ps -ef | grep byobu' and 'ps -ef | grep tmux'? Commented Aug 29, 2013 at 16:53

4 Answers 4

19
tmux kill-session [-t session_name]

the processes in the virtual terminals should receive SIGHUP.

3
  • 11
    but SIGHUP doesn't kill processes. it usually respanws them. Commented Jun 4, 2014 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
    Commented Jun 5, 2014 at 14:34
  • AndreaPavoni's and peterph's comments explain why the web server I'm running in tmux seems to survive kill-session.
    – Zach
    Commented Nov 17, 2023 at 9:17
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

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.

0
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
3
  • This doesn't wait for the processes to terminate. So it's a race between whether SIGINT or SIGHUP finishes first. Commented Mar 23, 2022 at 12:12
  • please edit rather than just pointout
    – droid192
    Commented Apr 6, 2022 at 10:15
  • This approach is not viable. Can't edit without rewriting your whole code. Commented Apr 7, 2022 at 1:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .