Say you have plenty of interactive shells being run under same user id, like lots of screen
or tmux
(or both) "tabs". You tried unmount
'ing a device and realised you couldn't since one of those shell sessions had its current directory changed there. You can easily find TTY-name and list of processes associated with (just another bash
or zsh
), but assuming you don't want just kill
it until you're sure it's safe to do that, how are you going to find corresponding screen
/tmux
"tab"?

- 6,231
1 Answers
Not sure if this is the optimal way, but here is "a" way.
First, identify which "screen" or "tmux" session has the device open:
lsof -R $mountpoint
The few things you need from this output is the PID of the process, and its PPID (assume you assign these to $PID
and $PPID
respectively).
Next, check what child processes are running under this PID, this should help you out in case you ssh
ed into another machine from this shell, or you have an editor or something else running at the moment. If something is running, it should be easy enough for you to find the tab that you need based on the child commands.
pstree -p $PID
Assuming the above command produces no output, you should now check what is the parent of that shell.
ps -f $PPID
If this is anything but SCREEN
or tmux
you should be able to easily figure it out.
In case it is SCREEN
and you have multiple ones, you should be able to figure which one by looking at the child processes of the SCREEN
you need.
pstree -p $PPID
If there are still multiple ones, you can open up a new tab in each SCREEN
and keep rerunning pstree -p $PPID
until you find out which one it is. After that, you have to check each tab and somehow figure out the correct one. In the shell, you can check the PID of the shell if it matches the $PID
of the problematic one, or one of its children as produced by pstree -p $PID
above.
If it is tmux
, it is not so easy as all the shells are under a single tmux. However, something you can do with tmux
is automatically send keypressess to all open panes. These could do something unexpected if you have an editor running in there, but you probably know best if it is okay to do this or not.
for i in $(tmux list-panes -a | awk -F': ' '{print $1}'); do
tmux send-keys -t $i "[[ \$\$ == $PID ]] && logout" Enter
done

- 226
-
Dam'it, man you've spent lots of time to write it all down. ;) – poige Jul 04 '16 at 16:38