如何在使用损坏的 CTRL+Z 执行后将进程置于后台?
这个问题很特殊,因为某些键(例如 CTRL+Z)停止工作。
我尝试通过输入以下顺序将进程置于后台:
- 查找/
- CTRL+Z
- 背景
但是,我仍然可以看到标准输出。 与只执行第一步的唯一区别是命令 CTRL+Z 不再起作用。 当我有未保存的作业并且我的硬盘超过 100GB 时,这是相当令人讨厌的。 所以
如何将进程置于后台?
[详细信息]
我正在 Mac 上使用 Bash 的第四个版本。
[Nicholas Riley 的关键回复]
问题确实在于我不了解运行进程后台的“后果”。 我不明白为什么 CTRL+Z 等命令不适用于后台进程。 我仍然能够使用以下命令在另一个 shell 中终止该进程:
ps -ej | awk '! /grep/ && /find/ {print $2}' | xargs kill -9
The question is special because some keys, such as CTRL+Z, stopped working.
I tried to put the process to background by typing in the order:
- find /
- CTRL+Z
- bg
However, I can still see the stdout. The only difference to only doing the first step is that the command CTRL+Z does not work anymore. It is rather nasty when I have unsaved jobs and my harddrive is over 100GB. So
how can I put the process to background?
[Details]
I am using the fourth version of Bash on Mac.
[Crux Reply by Nicholas Riley]
The problem is really that I do not understand the "ramifications" of running process background. I cannot understand why the commnands, such as CTRL+Z, do not work to background processes. I was still able to kill the process in another shell with the command:
ps -ej | awk '! /grep/ && /find/ {print $2}' | xargs kill -9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
^Z
不起作用,因为最前面的工作现在是 shell,而 shell 通常不会响应SIGTSTP
。 (如果您确实想挂起非登录 shell,挂起
通常有效。)问题似乎是您误解了后台作业的后果。 重定向作业的标准输出是不相关的。
事实上,您并不清楚要做什么。 如果您只是想停止
find
运行,则fg
它并使用^C
或^\
(其中默认情况下分别发送SIGINT
和SIGQUIT
)。如果您想保持 find 运行但抑制其进一步输出,那么我能想到的最简单的解决方案是使用 bg ; 否认; 退出。 这将阻止 shell 终止其子进程 (
find
) 并退出 shell; 假设它位于终端窗口的顶层,您将看到更多输出,并且find
将继续运行(但您无法与其交互)。^Z
isn't working because the frontmost job is now the shell, and shells don't usually respond toSIGTSTP
. (If you do really want to suspend a non-login shell,suspend
usually works.)The problem seems to be you misunderstand the ramifications of a job being in the background. Redirecting the job's standard output is unrelated.
In fact, it's unclear what you want to do. If you just want to stop
find
from running, thenfg
it and use^C
or^\
(which by default sendSIGINT
andSIGQUIT
respectively).If you want to keep
find
running but suppress its further output, then the easiest solution I can think of is to usebg ; disown ; exit
. That will stop the shell from killing its child process (find
) and exit the shell; assuming it's at the top level of the Terminal window, you'll see a bit more output andfind
will keep running (but you'll have no way to interact with it).我用否认。
您也可以在 ^Z 之后使用 disown:
我相信 disown 是 bash 内置命令。 不确定其他外壳的替代品。
有关更多信息,请参阅我对服务器故障的等效答案< /a>.
I use disown.
You can use disown after you ^Z as well:
disown is a bash builtin, I believe. Not sure about alternatives for other shells.
For further information, see my equivalent answer on Server Fault.
您可以在任何类 UNIX 系统上使用 stty 将操作设置为不同的键。
将使 Q 代替 CTRL-Z 成为暂停键。
You can set the operation to a different key with stty on any UNIX-like system.
will make Q your suspend key in place of CTRL-Z.
后台进程不会断开与终端设备的输出,这就是为什么您仍然看到输出,并且输出很可能包含控制字符,这些字符可能会填满您的 TTY 设置(
cat
ing 二进制文件是一个这样做的好方法)。如果您希望作业在后台运行,请正确执行:
然后在完成后检查
/tmp/out
文件。Backgrounding a process will not disconnect the output from the terminal device, that's why you're still seeing output and the output may well contain control characters which can stuff up your TTY settings (
cat
ing binary files is a good way to do that).If you want the job to run in the background, do it right:
then examine the
/tmp/out
file when it's finished.我通常用 nohup 来做这种事情。 像这样:
nohup find /> /tmp/myresults.txt &
nohup 确保进程不会停止,即使控制台消失(例如,您关闭窗口或丢失 SSH 或其他什么)。 “>” 将输出发送到文件而不是控制台,并且“&” 将作业置于后台。
I usually do this kind of thing with nohup. Like this:
nohup find / > /tmp/myresults.txt &
nohup makes sure that the process doesn't stop, even if the console goes away (like, you close the window or lose your SSH or whatever). The ">" sends output to a file rather than to the console, and "&" puts the job in the background.
我假设您使用的是 Linux 或 UNIX 的常见变体,也可能使用 bash shell。 在这种情况下:
CTRL-Z 向前台进程发送
SIGTSTP
信号。 您应该能够通过指定kill -s SIGTSTP [pid] 来使用kill 命令执行相同的操作。I'll assume you're using a common variant of Linux or UNIX and possibly the bash shell. In which case:
CTRL-Z sends the
SIGTSTP
signal to the foreground process. You should be able to do the same thing with the kill command by specifyingkill -s SIGTSTP [pid]
.杀死 -STOP your_pid_here
从另一个控制台窗口
? http:// tombuntu.com/index.php/2007/11/23/how-to-pause-a-linux-process/#comment-132413
kill -STOP your_pid_here
from another console window?
http://tombuntu.com/index.php/2007/11/23/how-to-pause-a-linux-process/#comment-132413
我知道在 Ubuntu 9.04 中,您可以使用 & 启动该进程。 之后在后台运行它。 例如,“sudo gedit /boot/grub/menu.lst &” 将启动一个文本编辑器在后台编辑 GRUB 配置文件。
I know that in Ubuntu 9.04, you can start the process with a & after it to run it in the background. For example, "sudo gedit /boot/grub/menu.lst &" will start a text editor editing the GRUB config file in the background.