Skip to main content
GameDev.net gamedev.net
🔒 Locked

Linux: send signal to stop process

Started by polyfrag Dec 29, 2012 at 9:57 AM 2 replies 3.1k views
Original Post
polyfrag
polyfrag

How can I send my process to stop gracefully?

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

myProcess stop

Cornstalks
Cornstalks
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.
santa01
santa01

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

Bregma
Bregma
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.