Linux: send signal to stop process

Started by
2 comments, last by Bregma 11 years, 3 months ago

How can I send my process to stop gracefully?

I want to be able to call it in the console:

myProcess stop

Advertisement
That is entirely dependent on how your program works and where it processes that input. Some options (assuming C):
exit(0);
kill(getpid(), SIGTERM); // or, to send it to the group: kill(-getpid(), SIGTERM);
kill(getpid(), SIGKILL); // or, to send it to the group: kill(-getpid(), SIGKILL);
return 0; // from the main() function
throw std::exception(); // assuming C++, and that you catch (std::exception) somewhere appropriate (like the end of your main() function)

It also depends on what you mean by "graceful," as some will argue that exit(0); is not graceful because it doesn't allow you to do any clean up, whereas others will argue it is graceful because the OS will, in some way, do any cleanup you failed to do.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

[quote name='Cornstalks' timestamp='1356775436' post='5015383']
kill(getpid(), SIGTERM);
[/quote]

I'd suggest also to install a handler for this signal to perform any manual cleanups like finishing network sessions properly, dump config files etc.

How can I send my process to stop gracefully?

I want to be able to call it in the console:


I'm not sure what you mean by "call it in the console" means, but the shell command you would use to stop a process is the kill command.

kill -HUP $pid # sends the hangup signal

or

kill -TERM $pid # sends the terminate signal (same as control-C)

where $pid is the process ID of your process.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement