Linux 相当于命令中的 ctrl c 取消程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/514771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of ctrl c in command to cancel a program
提问by Eduardo
I am running a long linux program in a remote machine, and I want to stop it, but my problem is that if I use the kill command then the program will exit without saving results. Normally what I do to finish the program is use Ctrl+Cand in that case the program saves the results, but right now I am not in the machine that is running the session so I cannot press Ctrl+C.
我在远程机器上运行一个很长的 linux 程序,我想停止它,但我的问题是,如果我使用 kill 命令,那么程序将退出而不保存结果。通常我完成程序的方法是使用Ctrl+ C,在这种情况下程序会保存结果,但现在我不在运行会话的机器中,所以我不能按Ctrl+ C。
My question is: is there any way to do in a remote way the equivalent of Ctrl+C?
我的问题是:有没有办法以远程方式做等效于Ctrl+ 的事情C?
采纳答案by Firas Assaad
Try:
尝试:
kill -SIGINT processPIDHere
Basically Ctrl Csends the SIGINT
(interrupt) signal while kill sends the SIGTERM
(termination) signal by default unless you specify the signal to send.
基本上Ctrl C发送SIGINT
(中断)信号,而 killSIGTERM
默认发送(终止)信号,除非您指定要发送的信号。
回答by Johan
ctrl c just sends a SIGINT signal, but there is some other signals that is a little more soft. http://www.gnu.org/software/libtool/manual/libc/Termination-Signals.html
ctrl c 只是发送一个 SIGINT 信号,但还有一些其他信号更柔和一些。http://www.gnu.org/software/libtool/manual/libc/Termination-Signals.html
I think that you can use the the kill command to send some other signal. (see man kill for more info)
我认为您可以使用 kill 命令发送一些其他信号。(有关更多信息,请参阅 man kill )
回答by dmckee --- ex-moderator kitten
If you control the long-running remote process, you could install a signal handler for SIGTERM (see man signal
and man sigaction
and the many SO questions on this topic), to cleanup nicely before dieing.
如果您控制长时间运行的远程进程,则可以为 SIGTERM 安装一个信号处理程序(请参阅man signal
和man sigaction
以及有关此主题的许多 SO 问题),以便在死之前进行很好的清理。
That is a very common thing to do.
这是很常见的事情。
回答by Ana Betts
Keep in mind as well in your signal handler, that it is like an interrupt handler in that you are verylimited as to what you are allowed to do in it without corrupting the rest of your program. The best thing you can do here is set an atomic_t "should_quit" variable.
在您的信号处理程序中也要记住,它就像一个中断处理程序,因为您在不破坏程序其余部分的情况下允许在其中执行的操作非常有限。在这里你能做的最好的事情是设置一个 atomic_t "should_quit" 变量。
回答by Michael Cole
Here's an example for mongod
这是 mongod 的示例
To start the daemon from the command line:
从命令行启动守护进程:
mongod &
Then later
然后后来
kill -SIGINT `pgrep mongod`
回答by Jeegar Patel
I am doing as below way
我按照下面的方式做
killall -2 <ProgramName>
or
或者
kill -2 <PID of your process>
I used to forget the the name of signal. i.e SIGINT/SIGKILL here so i am using number for that like killall -2 or killall -9
我曾经忘记了信号的名称。即 SIGINT/SIGKILL 在这里,所以我使用数字,例如 killall -2 或 killall -9