mintty
mintty copied to clipboard
Get mintty PID from bash?
Is it possible to determine from bash running in mintty (I'm running git-bash) - the process ID of the mintty under which bash is running?
[Some of the time you can get this from $PPID, but if you run a subshell, or worse if you start CMD.EXE from bash in mintty then start a second instance of bash from CMD, $PPID cannot refer to the mintty process ID.]
The process ID in cygwin does not match with the Win32 side.
@Biswa96 , thanks - yes I am aware of that, but in that case I can do cat /proc/$PPID/winpid
to get it. However, it still doesn't guarantee that I will get the PID of mintty.
You would climb up the hierarchy of PPID in the process list as long as the TTY remains the same, until you arrive at a process with a different TTY value - that's your terminal.
pid=$$
while [ $pid != 1 ]; do
pid=$(cat /proc/$pid/ppid)
cmdline=$(cat /proc/$pid/cmdline | tr '\0' ' ')
echo "$pid $cmdline [-W $(cat /proc/$pid/winpid)]"
case $cmdline in
*/bin/mintty*) break ;;
esac
done
echo "mintty_PID=$pid"
I recently did something similar which also works inside tmux, where mintty is not among the current shell's ancestors.
#!/usr/bin/env bash
# Guesses the PID of the mintty process which is a parent of the given PID (or the current PID, if none is given).
function findMinttyParentPid() {
local pid=$1; shift
local parentPName
while [ -e "/proc/$pid/ppid" ]; do
pid=$(<"/proc/$pid/ppid")
parentPName=/proc/$pid/exe
if [ -e "$parentPName" ]; then
parentPName=$(basename -- "$(realpath "$parentPName")")
if [ "$parentPName" = mintty ]; then
printf '%s\n' "$pid"
return 0
elif [ "$parentPName" = tmux ]; then
tmuxPid=$pid
fi
fi
done
return 1
}
if [ $# -eq 0 ]; then set -- $$; fi
for pid; do
tmuxPid=
findMinttyParentPid "$pid" && continue
# If this is running in tmux and there is exactly one other tmux process under a mintty, use that one
if [ -n "$tmuxPid" ]; then
otherTmuxPid=$(find /proc -mindepth 2 -maxdepth 2 -name exe -lname '*/tmux' -not -wholename /proc/$tmuxPid/exe)
otherTmuxPid=$(basename -- "$(dirname -- "$otherTmuxPid")")
findMinttyParentPid "$otherTmuxPid" && continue
fi
echo "No mintty parent process for $pid found." >&2
exit 1
done
This prints the Cygwin PID, so use e.g. pid=$(</proc/$(that_script_above)/winpid)
if desired.
What's the use case of this request?
Although I'm not the requester, this is what I use it for: In my zsh precmd
and preexec
functions, I pass mintty's Windows PID to an Autohotkey script. That uses this SetTaskbarProgress function to mark the mintty taskbar button green / red / default color when a foreground command is running / done with error status / done with status 0, respectively.
That can also be achieved with escape sequences: https://github.com/mintty/mintty/wiki/CtrlSeqs#progress-bar
What's the use case of this request?
So that I can read bash's current directory using the NT Native API (NtQueryInformationProcess
) then use this to set the directory in the mintty Window title. I also want to check the directory to see if it part of a git repo, then show the git branch in the Mintty window title bar.
My current way of doing this is to get bash to set the Window title to contain bash's PID on startup.
Yes, I know I can do this in PS1, but where I work a virus/threat checker is run for every executable that is started from bash, so running anything from within PS1 is slow enough to be irrirating (think 1.5 seconds to wait for each prompt).
NtQuery wants a Windows PID I suppose, which is different from the cygwin PID anyway, so that would not help. The window title can be set with escape sequences, including enhanced magic as you suggest (see bash PROMPT_COMMAND). Prompt delay may be slowed down by cygwin process startup time (the fork issue) but that can often be optimised by using alternative commands (e.g. bash substitution instead of basename).