如何在脚本中捕获'killall'命令的所有输出?

时间:2017-08-21 14:11:40

标签: bash

我最初有一个'killall'命令将输出重定向到一个文件,并在三个单独的运行中得到它:

No matching processes belonging to you were found
kill -TERM 7370
No matching processes belonging to you were found

我想要一个时间戳,所以我将脚本更改为:

d=$(date "+%Y-%m-%d %H:%M:%S : ")
r=$(killall osascript 2>&1)
echo "$d$r"
exit 0

现在我得到了这个(三个单独的运行):

2017-08-18 16:14:01 : No matching processes belonging to you were found
2017-08-18 16:29:01 :
2017-08-18 16:44:02 : No matching processes belonging to you were found

这是缺少kill -TERM nnnn详细信息。

如何捕获所有输出?

感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用选项-v, --verbose列出接收已发出信号的进程。 (见man killall

  

-v, - verbose

     

报告信号是否已成功发送。

因此,您的脚本应如下所示:

#!/bin/bash
d=$(date "+%Y-%m-%d %H:%M:%S : ")
r=$(killall -v osascript 2>&1)
echo "$d$r"
exit 0

osascript替换sleep会给我以下输出:

2017-08-21 17:14:30 : Killed sleep(12493) with signal 15