How to kill a windows process in a cygwin terminal?

19

9

I have a problem regarding how to kill a process in Cygwin on Windows. I am completely new to Cygwin. But I have a task that I simply cant kill. I have tried issuing the following commands:

kill 4568
kill -9 4568
/bin/kill -f 4568

I have issued the commands in a separate Cygwin terminal since I cannot Ctrl+C it in the Cygwin terminal where the process run. I have searched all over the internet without success.

user1093774

Posted 2012-06-01T11:25:00.033

Reputation: 299

What's the process you started, and how did you start it? – me_and – 2012-06-01T11:27:21.987

Answers

27

ps -W | awk '/calc.exe/,NF=1' | xargs kill -f

Or

ps -W | awk '$0~v,NF=1' v=calc.exe | xargs kill -f

Or

powershell kill -n calc

Steven Penny

Posted 2012-06-01T11:25:00.033

Reputation: 7 294

You may want to use ps -W | awk 'BEGIN{ IGNORECASE=1;} /calc.exe/,NF=1' | xargs kill -f because Windows is case-insensitive – tricasse – 2017-11-07T13:46:05.297

7

You may try:

taskkill /pid 4568

aggu

Posted 2012-06-01T11:25:00.033

Reputation: 142

1Bad answer, the OP asked for a Cygwin command. – Hashim – 2017-08-14T02:27:57.190

1It's taskkill //im <name-of-exe> (note the need for 2 slashes) – P.Brian.Mackey – 2018-02-28T16:35:09.117

taskkill /im:{name-of-executable} is useful as well. – LawrenceC – 2012-06-25T23:54:33.197

and taskkill /im <name-of-exe> /f is even more useful. – Erik Kaplun – 2012-09-16T15:36:14.393

5

If you want a BASH only solution, try this: (it works for me)

    KILLPS="<My Process Name>"
    WINPS=`ps -W | grep -i $KILLPS`         # Make case-insensitive.
    PID=`echo $WINPS | cut -d' ' -f1` 
    /bin/kill -f "$PID"

NOTE: use /bin/kill, the embedded shell kill will not kill PIDs for general windows proccesses.

dpminusa

Posted 2012-06-01T11:25:00.033

Reputation: 71

Your note about /bin/kill vs shell kill was very useful. Thanks – Phil – 2017-05-09T21:08:10.530

2

(From my answer to a similar question on SO):

Different Windows programs will handle the signals that kill sends differently; they've never been designed to deal with them in the same way that Linux/Cygwin programs are.

The only reliable method for killing a Windows program is to use a Windows specific tool, such as Task Manager or Process Explorer.

That said, if you've not already, you may have luck with running your Cygwin terminal in administrator mode (right click on your shortcut and select "Run as administrator").

me_and

Posted 2012-06-01T11:25:00.033

Reputation: 2 118

1

Two things to think about here:

  1. Get the correct PID, which is the WINPID.
  2. Use the right tool.

To get the correct WINPID to kill, use cat /proc/<PID>/winpid. I.e. run this:

ZID=$$; WINPID=$(cat /proc/${ZID}/winpid); echo "Kill WINPID: ${WINPID}"; ps; sleep 10 &

and immediately after do another ps.

The right tool to use is sysinternals PsKill64.exe -t <winpid> which also kills all descendants of the script process, which kill doesn't.

not2qubit

Posted 2012-06-01T11:25:00.033

Reputation: 1 234

1You got a vote for cat /proc/${ZID}/winpid, very good to know. And faster than solution with ps -aW | grep ..., at least on my cygwin under Win 7 x64. But for PsKill64 I think you need -t to kill sub processes, and taskkill can also kill subprocesses. – 244an – 2017-12-19T21:40:19.313

Yes, you also need the -t to kill descendants. Corrected answer. – not2qubit – 2017-12-20T14:47:53.147

0

If you have a Windows program that is a subprocess under a cygwin bash process you can use taskkill /F /PID [the cygwin process id] /T to kill the process tree, no need to get the Window PID from ps with awk etc.
This is tested under Win7 x64.

# Simple example in cygwin:
> notepad.exe &
> pid=$!
...
> taskkill /F /PID $pid /T

Of course you can use the Window PID also.

244an

Posted 2012-06-01T11:25:00.033

Reputation: 101

0

In Git Bash I use:

targetProcess='chromedriver.exe';
ps -W | grep -i ${targetProcess} | awk '{print $1}' | while read pid; do taskkill //PID ${pid}; done;

I have not tried it in Cygwin, but I suppose it will work there too.

Vagelis Prokopiou

Posted 2012-06-01T11:25:00.033

Reputation: 101