如何屏蔽kill输出
我运行如下脚本:
sleep 20 &
PID=$!
kill -9 $PID >/dev/null 2>&1
我不希望脚本显示如下输出:
line 51: 22943 Killed sleep
我不知道为什么会发生这种情况,我已将输出重定向到 /dev/null
I run a script like:
sleep 20 &
PID=$!
kill -9 $PID >/dev/null 2>&1
I dont want the script show the output like:
line 51: 22943 Killed sleep
I have no idea why this happen, I have redirect the output to /dev/null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该消息不是来自
kill
或后台命令,而是来自 bash,当它发现其后台作业之一已被终止时。要避免出现该消息,请使用disown
将其从 bash 的作业控制中删除:The message isn't coming from either
kill
or the background command, it's coming from bash when it discovers that one of its background jobs has been killed. To avoid the message, usedisown
to remove it from bash's job control:这可以使用“wait”+将 wait 重定向到 /dev/null 来完成:
此脚本不会给出“killed”消息:
同时,如果您尝试使用类似的内容:
它将输出消息:
我非常喜欢这个解决方案不仅仅是使用“否认”,这可能会产生其他影响。
想法来源:https://stackoverflow.com/a/5722850/1208218
This can be done using 'wait' + redirection of wait to /dev/null :
This script will not give the "killed" message:
While, if you try to use something like:
It will output the message:
I like this solution much more than using 'disown' which may have other implications.
Idea source: https://stackoverflow.com/a/5722850/1208218
禁用作业通知的另一种方法是将命令置于
sh -c 'cmd &'
构造中后台。Another way to disable job notifications is to put your command to be backgrounded in a
sh -c 'cmd &'
construct.我能够通过重定向在后台运行的命令的输出来完成此任务。在您的情况下,它看起来像:
...或者如果您不需要日志文件:
然后,当您想要终止该后台进程的 PID 时,它不会显示在标准输出上。
I was able to accomplish this by redirecting the output of the command that I am running in the background. In your case it would look like:
... or if you do not want a log file:
Then, when you want to kill that background process' PID, it will not show on standard out.