www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - system's "kill <pid>" signal

reply Konstantin Kutsevalov <adamasantares gmail.com> writes:
Hi,

is there a way to catch system signal of "kill" command or 
"shutdown"?

PS: are there some other ways also to send signals to running a D 
application?
Nov 04 2016
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 5 November 2016 at 02:24:00 UTC, Konstantin 
Kutsevalov wrote:
 Hi,

 is there a way to catch system signal of "kill" command or 
 "shutdown"?

 PS: are there some other ways also to send signals to running a 
 D application?
have a look in std.process I don't think you can catch kill.
Nov 04 2016
prev sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Saturday, 5 November 2016 at 02:24:00 UTC, Konstantin 
Kutsevalov wrote:
 Hi,

 is there a way to catch system signal of "kill" command or 
 "shutdown"?
During the Run-time: ==================== You can register a signal callback, like in this sample (hit CTRL+C once running in a terminal): import std.stdio; import core.sys.posix.signal; bool doQuit; extern(C) void handler(int num) nothrow nogc system { printf("Caught signal %d\n",num); doQuit = true; } void main(string[] args) { signal(SIGINT, &handler); while(true) { import core.thread; Thread.sleep(dur!"msecs"(50)); if (doQuit) break; } } After termination: ================== if (tryWait(PID)[0] == true) then the value carried by tryWait(PID)[1] will tell you if the process has exited because of a signal whatever it's, e.g SIGKILL, SIGTERM, SIGINT, ...
Nov 04 2016
next sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Saturday, 5 November 2016 at 06:17:51 UTC, Basile B. wrote:
 On Saturday, 5 November 2016 at 02:24:00 UTC, Konstantin 
 Kutsevalov wrote:
 Hi,

 is there a way to catch system signal of "kill" command or 
 "shutdown"?
During the Run-time: ==================== You can register a signal callback, like in this sample (hit CTRL+C once running in a terminal): import std.stdio; import core.sys.posix.signal; bool doQuit; extern(C) void handler(int num) nothrow nogc system { printf("Caught signal %d\n",num); doQuit = true; } void main(string[] args) { signal(SIGINT, &handler); while(true) { import core.thread; Thread.sleep(dur!"msecs"(50)); if (doQuit) break; } } After termination: ================== if (tryWait(PID)[0] == true) then the value carried by tryWait(PID)[1] will tell you if the process has exited because of a signal whatever it's, e.g SIGKILL, SIGTERM, SIGINT, ...
3rd option, from my Windows times I remember that people tend to use launchers to handle the real application, i.e a process that launches the main process. Then the launcher can have a thread that checks the PID (like in "After Term..."). If SIGKILL isn't handled by a signal() callback then this could be an option. Do you have to check if a server crashes or something like that ?
Nov 05 2016
parent reply Konstantin Kutsevalov <adamasantares gmail.com> writes:
On Saturday, 5 November 2016 at 07:52:53 UTC, Basile B. wrote:
 On Saturday, 5 November 2016 at 06:17:51 UTC, Basile B. wrote:
 3rd option, from my Windows times I remember that people tend 
 to use launchers
 to handle the real application, i.e a process that launches the 
 main process. Then the launcher can have a thread that checks 
 the PID (like in "After Term..."). If SIGKILL isn't handled by 
 a signal() callback then this could be an option.

 Do you have to check if a server crashes or something like that 
 ?
not a system crash. just a signal of sistems shutdown or process kill.
Nov 06 2016
next sibling parent Konstantin Kutsevalov <adamasantares gmail.com> writes:
On Sunday, 6 November 2016 at 16:05:44 UTC, Konstantin Kutsevalov 
wrote:
 On Saturday, 5 November 2016 at 07:52:53 UTC, Basile B. wrote:
 On Saturday, 5 November 2016 at 06:17:51 UTC, Basile B. wrote:
 3rd option, from my Windows times I remember that people tend 
 to use launchers
 to handle the real application, i.e a process that launches 
 the main process. Then the launcher can have a thread that 
 checks the PID (like in "After Term..."). If SIGKILL isn't 
 handled by a signal() callback then this could be an option.

 Do you have to check if a server crashes or something like 
 that ?
not a system crash. just a signal of sistems shutdown or process kill.
*of system
Nov 06 2016
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/6/16 11:05 AM, Konstantin Kutsevalov wrote:
 On Saturday, 5 November 2016 at 07:52:53 UTC, Basile B. wrote:
 On Saturday, 5 November 2016 at 06:17:51 UTC, Basile B. wrote:
 3rd option, from my Windows times I remember that people tend to use
 launchers
 to handle the real application, i.e a process that launches the main
 process. Then the launcher can have a thread that checks the PID (like
 in "After Term..."). If SIGKILL isn't handled by a signal() callback
 then this could be an option.

 Do you have to check if a server crashes or something like that ?
not a system crash. just a signal of sistems shutdown or process kill.
You can catch signals, but some the OS does not let you catch. IIRC, Posix systems on shutdown first send a "TERM" signal (15) which is the default signal for the kill command line as well. If the process does not clean up within a certain timeout, then it is sent the "KILL" signal (9), which CANNOT be caught. What you want is to handle process cleanup on the TERM signal. -Steve
Nov 06 2016
prev sibling parent Konstantin Kutsevalov <adamasantares gmail.com> writes:
On Saturday, 5 November 2016 at 06:17:51 UTC, Basile B. wrote:
 On Saturday, 5 November 2016 at 02:24:00 UTC, Konstantin 
 Kutsevalov wrote:
 Hi,

 is there a way to catch system signal of "kill" command or 
 "shutdown"?
During the Run-time: ==================== You can register a signal callback, like in this sample (hit CTRL+C once running in a terminal): After termination: ================== if (tryWait(PID)[0] == true) then the value carried by tryWait(PID)[1] will tell you if the process has exited because of a signal whatever it's, e.g SIGKILL, SIGTERM, SIGINT, ...
Thank you!
Nov 06 2016