简体   繁体   English

在 bash 脚本中终止 tail -f 命令

[英]terminating a tail -f command in bash scripting

i have the following script.我有以下脚本。 i want to break the tail -f command using the ENTER key.我想使用 ENTER 键打破 tail -f 命令。 but it is not working:\ please help.但它不工作:\请帮助。

tail -f /var/log/auth.log
while true 
do
read -s -n 1 key  
if [[ $key == $'\x0a' ]]
then
    #id=$(pidof tail) 
    #sudo kill -9 $id 
        
    
    fi
done

also tried this and but not working as well也尝试过,但效果不佳

while ! tail -f /var/log/auth.log
do
read -s -N 1 -t 1 key
if [[ $key == $'\x0a' ]];        # if input == ENTER key
then
    id=$(pidof tail)
    sudo kill -9 $id      
fi
done

If you want the rest of your script to operate while the tail is still going, you need to put the tail in the background.如果您希望脚本的 rest 在tail仍在运行时运行,则需要将tail置于后台。 Using & does this.使用&执行此操作。

tail -f /var/log/auth.log & tail_pid=$!  # store PID of tail in $tail_pid
read _                                   # read until there's a carriage return
kill "$tail_pid"                         # end the tail_pid process

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM