bash 是否有一个 shell 命令可以杀死所有后台尾部进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15252556/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:45:36  来源:igfitidea点击:

Is there a shell command that will kill all background tail processes

bashkillpid

提问by doapydave

If I run a script that starts several processes with the &like

如果我运行与启动多个进程的脚本&

tail -f log file1 &
tail -f log file2 &

How can i shut them all down at once?

我怎样才能一次关闭它们?

采纳答案by cfi

You can refer to background jobs in your current shell with the %1, %2, ... idioms.

您可以使用%1, %2, ... 习语引用当前 shell 中的后台作业。

To my knowledge there's no such thing as a catch all; there's no %*or an equivalent.

据我所知,没有什么是包罗万象的。没有%*或等效的。

But you could shortcut with

但是你可以用捷径

kill %1 %2 %3 %4 %5 %6 %7 %8

Which would kill the first eight background processes still running in your current shell. That may or may not be a tail.

这将杀死仍在当前 shell 中运行的前八个后台进程。那可能是也可能不是tail

Be careful whom you kill ;-)

小心你杀的人;-)

If you have full control of the background processes this might be a safe bet for you. Since you mention that you want to do this from a shell script, and if the tails are the only background processes, then this is straightforward. Just make sure your shell script starts a subshell, so that it never affects the background processes of an interactive shell. For instance you could start your script with

如果您可以完全控制后台进程,这对您来说可能是一个安全的选择。由于您提到要从 shell 脚本执行此操作,并且如果tails 是唯一的后台进程,那么这很简单。只需确保您的 shell 脚本启动一个子 shell,这样它就不会影响交互式 shell 的后台进程。例如,您可以使用以下命令启动脚本

#!/usr/bin/bash

and set execute permission bits on the script and always call the script by name. In other words, you should not source script_filethat script.

并在脚本上设置执行权限位并始终按名称调用脚本。换句话说,你不应该source script_file那个脚本。

On the other hand, jim's answerto save the pids (process ids) of any process you are starting is a much more safe way of killing other processes.

另一方面,jim保存您正在启动的任何进程的 pid(进程 ID)的答案是一种更安全的杀死其他进程的方法。

回答by Vereb

You can kill all tail commands by killall tail

您可以通过以下方式杀死所有尾部命令 killall tail

回答by Cfreak

killall tailshould do the trick but it will also close any other tail processes you have running.

killall tail应该可以解决问题,但它也会关闭您正在运行的任何其他尾部进程。

回答by jim mcnamara

"remember" the child pids.

“记住”孩子的pid。

tail -f logfile1 & 
pid1=$!

tail -f logfile2 &
pid2=$!

kill $pid1 $pid2

Obviously, you would not issue an immediate kill like that in your real script. You would probably really want to wait for the child processes instead. But this does what you asked

显然,你不会在你的真实脚本中发出这样的立即终止。您可能真的想要等待子进程。但这可以满足您的要求