pidtmuxhtop

Find tmux session that a PID belongs to


I am using htop so see what processes are taking up a lot of memory so I can kill them. I have a lot of tmux sessions and lots of similar processes. How can I check which tmux pane a PID is in so I can be sure I am killing stuff I want to kill?


Solution

  • Given that PID in the below line is the target pid number:

    $ tmux list-panes -a -F "#{pane_pid} #{pane_id}" | grep ^PID
    

    The above will identify the pane where the PID is running. The output will be two strings. The first number should be the same as PID and the second one (with a percent sign) is "tmux pane id". Example output:

    2345 %30
    

    Now, you can use "tmux pane id" to kill the pane without "manually" searching for it:

    $ tmux kill-pane -t %30
    


    To answer your question completely, in order to find *tmux session* that a PID belongs to, this command can be used:

    $ tmux list-panes -a -F "#{pane_pid} #{session_name}" | grep ^PID
    # example output: 2345 development
    

    Here's another possibly useful "line":

    $ tmux list-panes -a -F "#{pane_pid} #{session_name}:#{window_index}:#{pane_index}" | grep ^PID
    # example output: 2345 development:2:0
    

    The descriptions for all of the interpolation strings (example #{pane_pid}) can be looked up in tmux man page in the FORMATS section.