Gracefully terminate a command line application on Windows - windows

I'm creating a command-line application, which spawns a process (command defined by a user, usually an HTTP server) and when the application's job is done, I want to let the process know it should terminate.
In UNIX, I can do that by sending SIGTERM and if the process doesn't end, then I can kill it brutally by SIGKILL.
In Windows, I struggle to find an alternative to the SIGTERM scenario. I learned there's taskkill /PID XXXX (without /f!), but
I found no information about what taskkill /PID XXXX does under the hood, hence I can't test it. I can't find how to handle whatever taskkill /PID XXXX sends on the process side.
It doesn't seem to work with commands in cmd.exe. I tried to run a simple server process in one cmd.exe, get its PID and in another window to taskkill /PID XXXX it, but taskkill refused to do that: ERROR: The process with PID XXXX could not be terminated. Reason: This process can only be terminated forcefully (with /F option).
So my question is: How to inform a command-line process in Windows that it should terminate without forcefully terminating it? How to receive and act upon such message on the process-to-be-terminated side?

GenerateConsoleCtrlEvent signals a console application as if the user had pressed Ctrl+C or Ctrl+Break. Console applications can ignore these signals (using SetContolCtrlHandler), but, by default, they just do an ExitProcess on themselves.
This is the same signal Windows uses when the user closes the console window, logs off, or shuts down.

TaskKill uses PostMessage(hwnd, WM_CLOSE, 0, 0); (basically the same as pressing the X on a window) if the process has a visible window.
If you know the application is a console application you can use GenerateConsoleCtrlEvent instead.

Related

cmd batch - How to set the "Taskkill" command blocking or non-blocking in Windows command line?

We have a group of machines running scripts. Most of our machines fail to finish the task after last weekend.
After comparing the working machines and non-workings machines we find out something weired that the behavior of Taskkill command has changed from non-blocking command into a blocking one on broken machines.
For example, to kill a process:
taskkill /im notexist.exe /f, if the process 'notexist.exe' does not exist, command will return immediatelly on working machines but keep a 5s timeout before return on broken machines.
They share the same harware and os version and the 'taskkill.exe' version.
The blocking one:
The non-blocking one:
Is there anything in the registry matters? How could I make the behavior consistently?

Stop process/program without admin rights - python

Is it possible to end/stop/pause/freeze a process/program running in background with python without having administrator rights on a Windows 7/8/10? If yes how? If no why?
If it is yours, you can. Read about taskkill, taskmgr and wmic. Call them using os.system. Also, you may use os.kill, like:
os.kill (your-process-id, signal.SIGKILL)
(determining process ID requires reading process table, either parsing tasklist output or doing it via API)
If it is not yours, sorry... It means you should not kill it. But always, there is a way to kill the process - shut down the computer.
That depends.
If you have the rights to end a process running in background, you can do so.
You can execute commands (e.g. for killing) in python like in cmd by using the call method of the subprocess module.
from subprocess import call
call(['command','with','parameters'])
The command to kill a program in Windows is taskkill.
So to kill iexplore.exe by its name u use taskkill /F /IM iexplore.exe in cmd and call(['taskkill','/F','/IM','iexplore.exe']) in python code.
The documentation of call can be found here: https://docs.python.org/2/library/subprocess.html#subprocess.call
Manual of taskkill: https://technet.microsoft.com/de-de/library/bb491009.aspx
Hope this helps ^^

Why can't kill the process with taskkill?

how to kill the process
>netstat -nao
Active Connections
Proto Local Address Foreign Address State PID
TCP 10.10.8.10:50931 122.109.119.13:22 ESTABLISHED 4664
>taskkill /pid 4664
SUCCESS: Sent termination signal to the process with PID 4664.
>netstat -nao
Active Connections
Proto Local Address Foreign Address State PID
TCP 10.10.8.10:50931 122.109.119.13:22 ESTABLISHED 4664
why i can not kill pid 4664 with taskkill /pid 4664
Try passing the following parameters to TASKKILL:
/T = kill child process
/F = forceful termination
Another option would be to use Power Shell to kill by pid:
kill -id 4664
If nothing works, maybe the process is blocked waiting for some resource (perhaps a bug). Consider rebooting.
Sorry if this is too elementary, but you may want to
try looking under some of your open windows for a system message..
Ie "do you want to save"
Windows might be being polite and not closing the app before giving you a chance to save or don't save.
it also may be on the screen of the remote computer.

bash & linux symbols, Jboss

I'm writing a script to start Jboss, load an application, send requests to the application, shutdown jboss and repeat. However I dont know how to shut Jboss down from the script. At the moment I'm using
pkill -9 java
But I dont think this is right, because it kills the process, not shut it down. Is there a way to shut it down similar to pressing CTRL-C?
You want a simple
pkill java
From the man page:
pkill will send the specified signal (by default SIGTERM) to each
process
SIGTERM will send a termination signal to the process. If the process is well-written, it will catch this signal and perform an orderly shutdown. If that fails, that's when you can use SIGKILL (-9) which is a forceable termination with no chance for the process to catch and perform cleanup.
Never use kill -9 <PID> by default. It breaks things up, like file descriptors and such.
Start to run kill <PID> alone, default is -15 signal.
See
man 7 signal
And In what order should I send signals to gracefully shutdown processes?
NOTE
kill or pkill doesn't change things so much, same signals are trigered
What you actually want is:
pkill -f jboss
using pkill java could kill any other processes using java on the box.

How to kill an open process on node.js?

I'm trying to set up a build-system for Node.js on sublime, so I can press F7 to call "node" on the openned file. The problem is that the process is then open forever, so, the second time I use F7 I get an add-in-use.
Is there a way I can kill the openned "node.exe" process from node.js?
Use the following set of commands to identify the process running on a given port and to termiate it from the command line
sudo fuser -v 5000/tcp // gives you the process running on port 5000
It will output details similar to the one shown below
USER PID ACCESS COMMAND
5000/tcp: almypal 20834 F.... node
Then use
sudo fuser -vk 5000/tcp
to terminate the process. Check once again using
sudo fuser -v 5000/tcp
to ensure that the process has terminated.
On Windows you could use the following steps
C:\> tasklist // will show the list of running process'
Image Name PID Session Name Session# Mem Usage
System 4 console 0 236 K
...
node.exe 3592 console 0 8440 k
Note the PID corresponding to your node process, in this case 3592. Next run taskkill to terminate the process.
C:\> taskkill /F /PID 3592
Or /IM switch
C:\> taskkill /F /IM node.exe
From within Node.js:
var die = function(quitMsg)
{
console.error(quitMsg)
process.exit(1);
}
die('Process quit');
There are certain methods available for exiting that are only available for POSIX (i.e. not Windows) that will exit a process by its process id.
Also, note that you might be able to send a kill() signal using this method, which does not say it isn't available for Windows:
process.kill(pid, [signal])
If you want to kill all processes than:
sudo killall -9 node
If you want to kill process on selected port than:
sudo kill sudo lsof -t -i:3100
That was port 3100
If sublime you say is sublimeText plugin, I have the same issue, and send TCP server a message 'shutdown' from python code, then
app.js
TCPserver
.on('connection', function(socket)
{
socket.pipe(require('through')
(function(data)
{ //----------------------------
if (data.toString() === 'shutdown')
{
process.exit();
}
//--------------------------
}));
socket.end();
})
Similarly to what #Alex W said, you can send a kill signal to the process so long as you have its process ID, or PID using the following node function:
process.kill(pid, [signal])
In my case, I had the PIDs readily available as I was spawning child_process().spawn.pid. I have tested it and it does work on Win 7 x64.

Resources