mirror of https://github.com/winsw/winsw
Making Ctrl+C the default behaviour
also making the timeout customizable, plus documentationpull/37/merge
parent
1ed045b817
commit
ae1bc43cff
16
Main.cs
16
Main.cs
|
@ -337,34 +337,26 @@ namespace winsw
|
||||||
StopProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
|
StopProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var proc = Process.GetProcessById(pid);
|
var proc = Process.GetProcessById(pid);
|
||||||
if (descriptor.SendSIGINT)
|
|
||||||
{
|
|
||||||
WriteEvent("Send SIGINT " + process.Id);
|
WriteEvent("Send SIGINT " + process.Id);
|
||||||
bool successful = SigIntHelper.SendSIGINTToProcess(proc);
|
bool successful = SigIntHelper.SendSIGINTToProcess(proc,descriptor.StopTimeout);
|
||||||
if (successful)
|
if (successful)
|
||||||
{
|
{
|
||||||
WriteEvent("SIGINT to" + process.Id + " successful");
|
WriteEvent("SIGINT to" + process.Id + " successful");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
WriteEvent("SIGINT to " + process.Id + " failed - Killing as fallback");
|
WriteEvent("SIGINT to " + process.Id + " failed - Killing as fallback");
|
||||||
proc.Kill();
|
proc.Kill();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
WriteEvent("ProcessKill " + process.Id);
|
|
||||||
proc.Kill();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (ArgumentException)
|
catch (ArgumentException)
|
||||||
{
|
{
|
||||||
// Process already exited.
|
// Process already exited.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void WaitForProcessToExit(Process process)
|
private void WaitForProcessToExit(Process process)
|
||||||
{
|
{
|
||||||
|
|
|
@ -185,6 +185,13 @@ When you use the `<stopargument>`, you must use `<startargument>` instead of `<a
|
||||||
<stopexecutable>catalina.sh</stopexecutable>
|
<stopexecutable>catalina.sh</stopexecutable>
|
||||||
<stopargument>stop</stopargument>
|
<stopargument>stop</stopargument>
|
||||||
|
|
||||||
|
### stoptimeout
|
||||||
|
When the service is requested to stop, winsw first attempts to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms683155(v=vs.85).aspx">send Ctrl+C signal to the process</a>, then wait for up to 15 seconds for the process to exit by itself gracefully. A process failing to do that (or if the process does not have a console), then winsw resorts to calling <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686714(v=vs.85).aspx">TerminateProcess</a> API to kill the service instantly.
|
||||||
|
|
||||||
|
This optional element allows you to change this "15 seconds" value, so that you can control how long winsw gives the service to shut itself down. See `<onfailure>` below for how to specify time duration:
|
||||||
|
|
||||||
|
<stoptimeout>10sec</stoptimeout>
|
||||||
|
|
||||||
### env
|
### env
|
||||||
This optional element can be specified multiple times if necessary to specify environment variables to be set for the child process. The syntax is:
|
This optional element can be specified multiple times if necessary to specify environment variables to be set for the child process. The syntax is:
|
||||||
|
|
||||||
|
|
|
@ -562,13 +562,13 @@ namespace winsw
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// True if the service can interact with the desktop.
|
/// Time to wait for the service to gracefully shutdown before we forcibly kill it
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool SendSIGINT
|
public TimeSpan StopTimeout
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return dom.SelectSingleNode("//sendsigint") != null;
|
return SingleTimeSpanElement(dom, "stoptimeout", TimeSpan.FromSeconds(15));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ namespace winsw
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="process">The process to attach to and send the SIGINT</param>
|
/// <param name="process">The process to attach to and send the SIGINT</param>
|
||||||
/// <returns>True if the process shut down successfully to the SIGINT, false if it did not.</returns>
|
/// <returns>True if the process shut down successfully to the SIGINT, false if it did not.</returns>
|
||||||
public static bool SendSIGINTToProcess(Process process)
|
public static bool SendSIGINTToProcess(Process process, TimeSpan shutdownTimeout)
|
||||||
{
|
{
|
||||||
if (AttachConsole((uint)process.Id))
|
if (AttachConsole((uint)process.Id))
|
||||||
{
|
{
|
||||||
|
@ -49,7 +49,7 @@ namespace winsw
|
||||||
SetConsoleCtrlHandler(null, true);
|
SetConsoleCtrlHandler(null, true);
|
||||||
GenerateConsoleCtrlEvent(CtrlTypes.CTRL_C_EVENT, 0);
|
GenerateConsoleCtrlEvent(CtrlTypes.CTRL_C_EVENT, 0);
|
||||||
|
|
||||||
process.WaitForExit(15000);
|
process.WaitForExit(shutdownTimeout.TotalMilliseconds);
|
||||||
|
|
||||||
return process.HasExited;
|
return process.HasExited;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue