Jump to content

  • Log In with Google      Sign In   
  • Create Account


Linux: send signal to stop process


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 polyfrag   Crossbones+   -  Reputation: 1825

Like
0Likes
Like

Posted 29 December 2012 - 03:57 AM

How can I send my process to stop gracefully?

 

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

 

myProcess stop



Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6974

Like
4Likes
Like

Posted 29 December 2012 - 04:03 AM

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, 29 December 2012 - 04:08 AM.

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

#3 santa01   Members   -  Reputation: 307

Like
3Likes
Like

Posted 29 December 2012 - 04:44 AM

kill(getpid(), SIGTERM);

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.



#4 Bregma   Crossbones+   -  Reputation: 5008

Like
3Likes
Like

Posted 29 December 2012 - 08:37 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS