Alternative to `pkill` or `pgrep -f` on cygwin?

14,767

Solution 1

pkill and pgrep certainly exist within Cygwin, in the procps package (you can search cygwin packages here).

It appears to work for me,

tony:~$ nohup sleep 100983 &
[1] 5476
tony:~$ nohup: ignoring input and appending output to `nohup.out'
tony:~$
tony:~$ ps -ef | grep sleep
    tony    5476    2696   2  23:28:53 /usr/bin/sleep
tony:~$ pkill -f sleep
[1]+  Terminated              nohup sleep 100983
tony:~$

and

tony:~$ nohup sleep 837746 &
[1] 228
tony:~$ nohup: ignoring input and appending output to `nohup.out'
tony:~$ pgrep -f 837746
228
tony:~$ pkill -f 837746
[1]+  Terminated              nohup sleep 837746
tony:~$

Update: okay, tested with java. Executing java from the Cygwin command line does not result in the java string being placed into /proc/<pid>/cmdline, that just contains -bash. This appears to be a limitation of Cygwin.

tony:~$ java -Dsomething=valid -jar Captor.jar &
[1] 2700
tony:~$ ps -ef
     UID     PID    PPID TTY     STIME COMMAND
    tony    4164       1   ?    Aug 21 /usr/bin/mintty
    tony    4676    4164   0    Aug 21 /usr/bin/bash
    tony    5776    4676   0    Aug 23 /usr/bin/ssh
    tony    5148       1   ?  23:53:03 /usr/bin/mintty
    tony    5332    5148   1  23:53:03 /usr/bin/bash
    tony    5816       1   ?  00:04:16 /usr/bin/mintty
    tony    5432    5816   2  00:04:16 /usr/bin/bash
    tony    2700    5432   2  00:04:34 /cygdrive/c/Windows/system32/java
    tony    2232    5432   2  00:04:39 /usr/bin/ps
tony:~$ pgrep -f something
tony:~$ cat /proc/2700/cmdline
-bash
tony:~$
tony:~$ cat /proc/2700/exename
/cygdrive/c/Windows/system32/java

I suspect therefore, the answer is you can't do this with a Cygwin tool, you would need a Windows specific tool.

Solution 2

In older Cygwin versions, there is a procps package that you can install to get the same functionality with the same commands.

In newer Cygwin versions, the procps package no longer exists and is replaced by package procps-ng.

Solution 3

The Cygwin utilities work like the Linux ones, but they only work on Cygwin processes. For Windows processes, you need to use Windows utilities, such as the ones from Sysinternals. PsKill can kill processes by name.

Share:
14,767

Related videos on Youtube

toolkit
Author by

toolkit

Updated on September 18, 2022

Comments

  • toolkit
    toolkit over 1 year

    Suppose I have started my process like:

    java -DSOME_STRING -jar foo.jar
    

    Then on linux, I can use the following to find or kill this process:

    pgrep -f SOME_STRING
    pkill -f SOME_STRING
    

    However this doesn't seem to work on cygwin. Can anyone suggest an alternative for these tools in cygwin?

    • Admin
      Admin over 12 years
      Doesn't work in what way? Doesn't kill the process, doesn't find the process to kill it? Gives an error?
    • Admin
      Admin over 12 years
      pgrep doesn't find the process. pkill doesn't kill the process.
    • Admin
      Admin over 9 years
      Here are implementations of killall and pidof crafted for Cygwin. Depends on Python 2/ - github.com/kata198/cygwin-ps-misc
  • tcoolspy
    tcoolspy over 12 years
    Your example is substantially different than the one the OP is trying. You matched the name of the process rather than an arbitrary string in it's arguments. Can you test the latter and see if that works?
  • EightBitTony
    EightBitTony over 12 years
    seems to work fine
  • RIQ
    RIQ about 12 years
    as can the native taskkill /f /im procname.exe
  • thameera
    thameera over 10 years
    It seems procps is not available for x64 cygwin.
  • golimar
    golimar over 9 years
    ps also shows Windows processes with the -W switch
  • Abdull
    Abdull over 7 years
    @thameera try package procps-ng.
  • Timo
    Timo over 6 years
    apt-cyg install procps-ng with newest cygwin
  • Jason
    Jason almost 6 years
    to install apt-cyg: wget https://raw.githubusercontent.com/transcode-open/apt-cyg/mas‌​ter/apt-cyg && install apt-cyg /bin
  • golimar
    golimar almost 3 years
    or tasklist | findstr procname