How to find which Tmux session a process belongs to?
Solution 1
lsof /path/to/.file.swp
will show the process ID of the offending vim process. If you want to write a script, use pid=$(lsof -Fp "$swp_file"); pid=${pid#p}
to get just the process ID.
Then ps 12345
where 12345 is the process ID will show some information about the process, in particular what tty it's running on (ps -o tty= -p $pid
in a script). The tty uniquely identifies a tmux window (assuming the process is running inside tmux), but I don't know how to go from the tty name to the tmux session.
What would give you the tmux session is the value of the TMUX
environment variable in the vim process. The session number is the last number, after the last comma.
Most unices have a way to find out the environment of a process, but the way differs between unix variants. On Linux, you can use </proc/$pid/environ grep -z '^TMUX='
to show the value of $TMUX
in process $pid
, so you can extract the session number as $(</proc/$pid/environ grep -z '^TMUX=' | sed 's/.*,//')
.
Solution 2
This shell snippet works pretty well for me (you'll need the pstree utility as well):
for s in `tmux list-sessions -F '#{session_name}'` ; do
echo -e "\ntmux session name: $s\n--------------------"
for p in `tmux list-panes -s -F '#{pane_pid}' -t "$s"` ; do
pstree -p -a -A $p
done
done
For two tmux sessions with two vim's each, I get this output:
tmux session name: 0
--------------------
zsh,3444
`-vim,3467 file_1
zsh,3474
`-vim,3495 file_2
tmux session name: 1
--------------------
zsh,3526
`-vim,3547 file_3
zsh,3554
`-vim,3576 file_4
Solution 3
Iterating using the response from Tor, a small bash snippet to get the session, window and pane where a particular PID is running:
export PID=1161102; for s in `tmux list-sessions -F '#{session_name}'` ; do
for p in `tmux list-panes -s -F '#{pane_pid}' -t "$s"` ; do
pstree -h -p -a -A $p | grep "[,]$PID" && tmux list-panes -s -t "$s" -f "#{==:#{pane_pid},$p}" -F '#{session_name}: #{window_index}.#{pane_index}'
done
done
Example output:
`-skydive,1161102 analyzer -c skydive.yml
SESSIONNAME: 2.3
Related videos on Youtube
David
Updated on September 17, 2022Comments
-
David over 1 year
I've got JS to run with a php upload form.
Everytime I go to upload a file to test this the file DOES upload but the progress bar doesn't work and I get an 'Upload Aborted' error message.
Below is the JS
function _(el) { return document.getElementById(el); } function uploadfile() { var file = _("file").files[0]; var formdata = new FormData(); formdata.append("file", file); var ajax = new XMLHttpRequest(); ajax.upload.addEventListener("progress", progressHandler, false); ajax.addEventListener("load", completeHandler, false); ajax.addEventListener("error", errorHandler, false); ajax.addEventListener("abort", abortHander, false); ajax.open("POST", "uploader.php"); ajax.send(formdata); } function progressHandler(event) { var percent = (event.loaded / event.total) * 100; _("progressbar").value = (percent); } function completeHandler(event) { _("status").innerHTML = "Upload Success"; _("progressbar").value = 0; } function errorHandler(event) { _("status").innerHTML = "Upload Failed"; } function abortHander(event) { _("status").innerHTML = "Upload Aborted"; }
This script attachs to an HMTL5
<progress>
with a default value of 0 and max of 100.Here is the php file to which I am linking the form to:
<?php $email = $_POST['email']; $name = $_POST['name']; $spc = '[email protected]'; $message = $_POST['message']; $size = $_FILES['file']['size']; $file = $_FILES['file']['tmp_name']; $filename = $_FILES['file']['name']; $filetype = $_FILES['file']['type']; $link = 'http://www.spcink.com/uploads/' . $email . '%7c' . $name; if(!empty($file)) { mkdir('../uploads/' . $email . '|' . $name, 0777, true); move_uploaded_file($file, '../uploads/' . '/'. $email . '|' . $name . '/' . $filename); mail($spc, 'File upload from:' . $name, $message . $link); } ?>
I can't seem to figure out what is wrong here. Could some one please look this over and help me out! Thank you!
-
lkraav over 13 yearsyes indeed, this basically works, at least for scenario when original vi process isn't sudoed. /proc/$pid/environ was new to me, good thinking on that. getting the session value as a number is good enough since the session id letters >9 simply increase alphabetically.
-
lkraav over 13 yearswhen using sudo to launch the hypothetical vi process to find session by, it looks like it needs to have sudo -E for the TMUX variable to travel into it's environ.
-
Gilles 'SO- stop being evil' over 13 years@lkraav: Best: use
sudoedit
instead ofsudo vim
. Also useful to know for other cases: either usesudo -E
or putDefaults !env_reset
in/etc/sudoers
. In a pinch: find out the parent process ofvim
, which is likely to be a shell running in the same tmux window, and get itsTMUX
variable. -
akavel over 10 yearsgreat, thanks! also, adding
-h
inpstree
call will highlight the current pane, FWIW; or, passing-P $PID
instead should highlight some $PID you might be searching. But you don't really need this, I suppose. -
lucidbrot over 4 yearsAccessing
/proc/pid/environ
did not work for me using sudo, but withcat
it works