You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+87-2Lines changed: 87 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,26 +22,64 @@ To read a detailed info please visit [windows-kill-library Readme](https://githu
22
22
23
23
### Limitations
24
24
To send the signal, **windows-kill** at first send a same signal to the process that is calling it, to find a thread address. Then the founded address is used to send the real signal. Because of this, the process that is sending the signal will get the same signal too. But windows-kill register a signal handle during this procedure, so the process will not terminate. But if the process that is sending signal has child process, or is a child process of another process, sending signal will trigger the signal handles in other process in the same process group. And the default behavior of Windows console/application in case of getting a ```SIGINT``` or ```SIGBREAK```, is to terminate.
25
+
25
26
To **sum up**, If you are sending signal in node app that has child process (any kind of it), or is a child process of another process, the result is the termination of all the processes in the same process group, except the sender (well if it's a child process, because the master is terminated, it will terminate too).
26
27
27
28
**PS**: Currently there is no solution for this problem. But I'm working on it, to find a solution.
28
29
30
+
**PS-1**: A solution for parent processes that wants to send signal (no way for child processes currently), is added. It's setting the ```warmUp: true``` option when first calling the windows-kill in parent, before any child processes creation.
31
+
29
32
## Usage
30
33
```windows-kill``` expose a function. Simply run the exposed function with/without the options. Thats it. It should be called before any usage of ```process.kill```.
31
34
32
35
By default, ```windows-kill``` will enhance the node's process.kill in a way, that no code changes are needed in your codebase. Enhance means that ```windows-kill``` will replace the node's ```process.kill``` with a custom function with the same arguments and functionality. Just some changes to achieve signaling in Windows.
33
36
34
37
### Simplest usage
38
+
The returned function from calling the exported function, could be used to send signal, just like the way you call ```process.kill```. It will accept two argument. a ```PID``` and a ```SIGNAL```.
39
+
35
40
```javascript
41
+
/*
42
+
Require and call the function that's exported.
43
+
The returned function can be used to send signal.
44
+
*/
36
45
var windowsKill =require('windows-kill')();
37
46
47
+
/*
48
+
By default, process.kill is enhanced (only in Windows OS),
49
+
so you can either call process.kill, or the returned function.
50
+
*/
51
+
process.kill(PID, SIGNAL);
52
+
windowsKill(PID, SIGNAL);
53
+
```
54
+
55
+
```javascript
56
+
/*
57
+
Just call the function, and no need to change any code.
58
+
Every process.kill calls in your code, will now enhanced.
59
+
Just need to call windows-kill, before any call of
60
+
process.kill.
61
+
*/
62
+
require('windows-kill')();
63
+
38
64
process.kill(PID, SIGNAL);
39
65
```
40
66
41
-
### Using Options
67
+
### Options
68
+
Options and default values are:
69
+
70
+
```javascript
71
+
constdefaultOptions= {
72
+
replaceNodeKill:true,
73
+
warmUp:false
74
+
};
75
+
```
76
+
77
+
#### replaceNodeKill
78
+
This option will tell the windows-kill that should it enhance/replace the nodes process.kill? By setting true (default value), the nodes process.kill will replaced by a custom function. This custom function will check the signal that is sending using process.kill. If the signal is a member of supported signals, which is ```SIGINT``` or ```SIGBREAK```, it will call the modules function that is responsible for sending signal. Otherwise, the signal and pid will be passed to the node's original process.kill.
79
+
42
80
```javascript
43
81
constoptions= {
44
-
replaceNodeKill:true// Should windowskill enhance/replace node's process.kill. Default: true
82
+
replaceNodeKill:true,// Should windows-kill enhance/replace node's process.kill? Default: true
45
83
};
46
84
47
85
require('windows-kill')(options);
@@ -59,6 +97,53 @@ var windowsKill = require('windows-kill')(options);
59
97
windowsKill(PID, SIGNAL);
60
98
```
61
99
100
+
#### warmUp
101
+
By setting warmUp to ```true```, windows-kill will find and save the ```ctrl-routine``` addresses, without any need to send signal. By default, the address will find, when the first signal of that type is sending. Future call will use the founded address.
102
+
103
+
```javascript
104
+
constoptions= {
105
+
warmUp:true, // Should windows-kill warm-up by finding the ctrl-routines addresses? Default: false
106
+
};
107
+
108
+
require('windows-kill')(options);
109
+
110
+
process.kill(PID, SIGNAL);
111
+
```
112
+
113
+
Warm-up, is one way to overcome the limitations. Setting this option, will make the windows-kill to find needed address, before any signal sending. As stated in limitations section, finding address will cause the processes that have child process, or is a child process, trigger the ```ctr-routine``` of all process group members, which means termination of all of them. But warm-up mechanism can be used, to fix the issue in parent process. By setting it to ```true```, before any child process creation, sending signal in future will use the save addresses and no need to find addresses again.
114
+
115
+
To clear it up, see below example:
116
+
117
+
```javascript
118
+
var options = {
119
+
warmUp:false
120
+
};
121
+
122
+
var cp =require('child_process');
123
+
var windowsKill =require('windows-kill')(options);
124
+
125
+
var cp1 =cp.spawn('node', ['cp.js']);
126
+
127
+
windowsKill(PID, 'SIGINT');
128
+
```
129
+
130
+
By running the above code, the cp1 child process will terminate. Because the ```SIGINT``` signal is sent for the first time, and the ```ctrl-routine``` address in not available. So windows-kill will try to find it, and trying to find it will trigger the ```SIGINT``` handler of cp1 child process, which lead to termination of cp1.
131
+
132
+
To solve the issue, we can set the ```warmUp``` option true. Just remember, the initialization of windows-kill with warmUp option should be done before any child process creation. Like below:
133
+
134
+
```javascript
135
+
var options = {
136
+
warmUp:true
137
+
};
138
+
139
+
var cp =require('child_process');
140
+
var windowsKill =require('windows-kill')(options);
141
+
142
+
var cp1 =cp.spawn('node', ['cp.js']);
143
+
144
+
windowsKill(PID, 'SIGINT');
145
+
```
146
+
62
147
## Contributing
63
148
We love contributions from everyone. Please read [Contributing guide](https://github.com/alirdn/node-windows-kill/blob/master/CONTRIBUTING.md).
0 commit comments