如何在使用损坏的 CTRL+Z 执行后将进程置于后台?

发布于 2024-07-18 06:55:49 字数 637 浏览 4 评论 0原文

这个问题很特殊,因为某些键(例如 CTRL+Z)停止工作。

我尝试通过输入以下顺序将进程置于后台:

  1. 查找/
  2. CTRL+Z
  3. 背景

但是,我仍然可以看到标准输出。 与只执行第一步的唯一区别是命令 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:

  1. find /
  2. CTRL+Z
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

草莓酥 2024-07-25 06:55:49

^Z 不起作用,因为最前面的工作现在是 shell,而 shell 通常不会响应 SIGTSTP。 (如果您确实想挂起非登录 shell,挂起 通常有效。)

问题似乎是您误解了后台作业的后果。 重定向作业的标准输出是不相关的。

事实上,您并不清楚要做什么。 如果您只是想停止 find 运行,则 fg 它并使用 ^C^\ (其中默认情况下分别发送 SIGINTSIGQUIT)。

如果您想保持 find 运行但抑制其进一步输出,那么我能想到的最简单的解决方案是使用 bg ; 否认; 退出。 这将阻止 shell 终止其子进程 (find) 并退出 shell; 假设它位于终端窗口的顶层,您将看到更多输出,并且 find 将继续运行(但您无法与其交互)。

^Z isn't working because the frontmost job is now the shell, and shells don't usually respond to SIGTSTP. (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, then fg it and use ^C or ^\ (which by default send SIGINT and SIGQUIT respectively).

If you want to keep find running but suppress its further output, then the easiest solution I can think of is to use bg ; 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 and find will keep running (but you'll have no way to interact with it).

望笑 2024-07-25 06:55:49

我用否认。

find / & disown
exit # close the terminal and the command still runs

您也可以在 ^Z 之后使用 disown:

find /
^Z
bg
disown
exit

我相信 disown 是 bash 内置命令。 不确定其他外壳的替代品。

有关更多信息,请参阅我对服务器故障的等效答案< /a>.

I use disown.

find / & disown
exit # close the terminal and the command still runs

You can use disown after you ^Z as well:

find /
^Z
bg
disown
exit

disown is a bash builtin, I believe. Not sure about alternatives for other shells.

For further information, see my equivalent answer on Server Fault.

终止放荡 2024-07-25 06:55:49

您可以在任何类 UNIX 系统上使用 stty 将操作设置为不同的键。

$ stty susp Q

将使 Q 代替 CTRL-Z 成为暂停键。

You can set the operation to a different key with stty on any UNIX-like system.

$ stty susp Q

will make Q your suspend key in place of CTRL-Z.

日裸衫吸 2024-07-25 06:55:49

后台进程不会断开与终端设备的输出,这就是为什么您仍然看到输出,并且输出很可能包含控制字符,这些字符可能会填满您的 TTY 设置(cating 二进制文件是一个这样做的好方法)。

如果您希望作业在后台运行,请正确执行:

find / >/tmp/out 2>&1 &

然后在完成后检查 /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 (cating binary files is a good way to do that).

If you want the job to run in the background, do it right:

find / >/tmp/out 2>&1 &

then examine the /tmp/out file when it's finished.

云巢 2024-07-25 06:55:49

我通常用 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.

吖咩 2024-07-25 06:55:49

我假设您使用的是 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 specifying kill -s SIGTSTP [pid].

平生欢 2024-07-25 06:55:49

我知道在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文