Sign in to follow this  
polyfrag

Linux: send signal to stop process

Recommended Posts

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. Edited by Cornstalks

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this