Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 监控bash脚本赢得';不要终止_Linux_Bash - Fatal编程技术网

Linux 监控bash脚本赢得';不要终止

Linux 监控bash脚本赢得';不要终止,linux,bash,Linux,Bash,我有一个bash脚本,它的任务是监视日志文件中某一行的出现。找到后,脚本将发出电子邮件警告,然后自行终止。出于某种原因,它一直在运行。如何确保在下面终止bash脚本: #!/bin/sh tail -n 0 -f output.err | grep --line-buffered "Exception" | while read line do echo "An exception has been detected!" | mail -s "ALERT" monitor@compan

我有一个bash脚本,它的任务是监视日志文件中某一行的出现。找到后,脚本将发出电子邮件警告,然后自行终止。出于某种原因,它一直在运行。如何确保在下面终止bash脚本:

#!/bin/sh

tail -n 0 -f output.err | grep --line-buffered "Exception" | while read line
do
    echo "An exception has been detected!" | mail -s "ALERT" monitor@company.com
    exit 0
done

您正在读取时在
中打开一个子shell,该子shell是正在退出的,而不是正确的子shell

在进入while循环之前请尝试:

SHELLPID=$$
然后在循环中:

kill $SHELLPID
exit 0
或者将循环更改为不使用子shell


因为父脚本总是在
尾部-f
中,它永远不会结束,我想你别无选择,只能从内部子shell杀死它。

你在读取
时在
中打开一个子shell,子shell是退出的人,而不是正确的人

在进入while循环之前请尝试:

SHELLPID=$$
然后在循环中:

kill $SHELLPID
exit 0
或者将循环更改为不使用子shell


因为父脚本总是在
尾部-f
中,它永远不会结束,所以我认为除了从内部子shell中删除它之外,您别无选择。

尝试以下方法:

tail -n 0 -f output.err | grep --line-buffered "Exception" | while read line do echo "An exception has been detected!" | mail -s "ALERT" monitor@company.com kill -term `ps ax | grep tail | grep output.err | awk '{print $1}'` done tail-n0-f output.err | grep--读取行时缓存的行“异常” 做 echo“检测到异常!”|邮件-s“警报”monitor@company.com kill-term`ps ax | grep tail | grep output.err | awk'{print$1}'` 完成
如果您只有一条尾巴关注这个特定的文件,那么这应该是可行的

试试这样的方法:

tail -n 0 -f output.err | grep --line-buffered "Exception" | while read line do echo "An exception has been detected!" | mail -s "ALERT" monitor@company.com kill -term `ps ax | grep tail | grep output.err | awk '{print $1}'` done tail-n0-f output.err | grep--读取行时缓存的行“异常” 做 echo“检测到异常!”|邮件-s“警报”monitor@company.com kill-term`ps ax | grep tail | grep output.err | awk'{print$1}'` 完成
如果您只有一条尾巴关注这个特定的文件,那么这应该是可行的

那没用$PPID返回起始PID(运行脚本的shell实例)。有没有办法通过while循环重写脚本?检查更新。最后,您仍然需要一个子shell,因为当其他进程检查输出时,进程需要等待尾部。nit:父进程不在tail-f中。管道的每个进程都作为一个单独的子进程执行,父脚本正在等待它们。您不需要将
$$
保存到另一个变量。它在子shell中不会改变(至少在这种情况下)。@Dennis我希望子shell的$$会有所不同,因为这是另一个进程,$$将返回另一个不起作用的PID$PPID返回起始PID(运行脚本的shell实例)。有没有办法通过while循环重写脚本?检查更新。最后,您仍然需要一个子shell,因为当其他进程检查输出时,进程需要等待尾部。nit:父进程不在tail-f中。管道的每个进程都作为一个单独的子进程执行,父脚本正在等待它们。您不需要将
$$
保存到另一个变量。它在子shell中不会改变(至少在这种情况下)。@Dennis我希望子shell的$$会有所不同,因为它是另一个进程,$$将返回另一个PID,这是过度杀戮(无意双关语)。这是过度杀戮(无意双关语)。