mintty icon indicating copy to clipboard operation
mintty copied to clipboard

Get mintty PID from bash?

Open Ams627 opened this issue 3 years ago • 10 comments

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.]

Ams627 avatar May 07 '21 13:05 Ams627

The process ID in cygwin does not match with the Win32 side.

Biswa96 avatar May 07 '21 13:05 Biswa96

@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.

Ams627 avatar May 07 '21 17:05 Ams627

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.

mintty avatar Jun 09 '21 20:06 mintty

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"

joeinwap avatar Sep 13 '21 01:09 joeinwap

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.

mkq avatar Mar 29 '23 09:03 mkq

What's the use case of this request?

mintty avatar Mar 29 '23 18:03 mintty

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.

mkq avatar Mar 30 '23 14:03 mkq

That can also be achieved with escape sequences: https://github.com/mintty/mintty/wiki/CtrlSeqs#progress-bar

mintty avatar Mar 30 '23 17:03 mintty

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).

Ams627 avatar Apr 05 '23 19:04 Ams627

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).

mintty avatar Apr 06 '23 07:04 mintty