Search code examples
bashprocesscygwinpid

Cygwin: How can I obtain the PID of a program started by CygStart?


If I open a CygWin Bash shell and do:

Luis@Kenobi ~/Temporal
$ notepad &
[1] 1880

Luis@Kenobi ~/Temporal
$ echo $!
1880

the PID variable $! is correct.
But if I do:

Luis@Kenobi ~/Temporal
$ cygstart notepad

Luis@Kenobi ~/Temporal
$ echo $!
[No results]

This time $!has no value at all.

How can I get the PID of the just started CygStart program?


Solution

  • You cannot. You can get the PID of the cygstart process via

    $ cygstart notepad &
    $ echo "$!"
    

    but this won't do you any good since I guess you want to have control over the notepad process. Since cygstart starts notepad via Windows' ShellExecuteW and because it consequently won't be a child process of cygstart/your current shell there is no way for you to kill it by killing cygstart.

    You can easily verify this with Process Explorer.