簡體   English   中英

如何在腳本中停止tail命令

[英]How do I stop tail command in script

我想做什么 - 使用自定義ssh客戶端代碼***開始捕獲CHANGES到日志文件。 經過一段時間(不是固定值並且基於事件),發出命令以停止拖尾。 這是我用來捕獲日志文件的最新更改的命令 - tail -f logfile.txt

我希望能夠用以下內容結束它:q我可以從腳本中發出。 我不想像ctrl + c這樣的鍵盤命令。

***我的自定義ssh客戶端代碼的偽代碼(用oop語言編寫)

include ssh-api
ssh = getSSHConnection();
cmd = 'cd to folder';
ssh.command(cmd);
cmd = 'tail -f log.txt';
ssh.command(cmd);
wait for special event to occur...
cmd = 'stop the tail now!'
out = ssh.command(cmd);
print "the changes made to log file were\n"  + out;

我沒有對日志文件所在服務器的寫訪問權限。

我嘗試了什么 - http://www.linuxquestions.org/questions/red-hat-31/how-to-stop-tail-f-in-scipt-529419/

我無法理解那里的解決方案(在帖子2中)。 有人可以解釋解決方案或建議采用不同的方法嗎?

謝謝。

在你的腳本里面你可以使用$! 變量。

# run tail -f in background
tail -f /var/log/sample.log > out 2>&1 &

# process id of tail command
tailpid=$!

# wait for sometime
sleep 10

# now kill the tail process
kill $tailpid

$! 擴展到最近執行的后台(異步)命令的進程ID。

我用一些方法來處理tail -f ,使用

等待具體的進入:

我經常使用這個:

sed -une '
    /DHCPOFFER/{
        s/^\(.*\) [^ ]\+ dhcpd: DHCPOFFER on \([0-9.]\+\) to \([0-9a-f:]\+\) .*$/\1 \3 -> \2/p;
        q;
    }' < <(
      tail -f /var/log/syslog
    )

使用此命令,我可以等待下一個dhcp客戶端並獲取事件,mac地址和提供的IP的時間。

在腳本中:

#!/bin/bash

read rMac rIp rDate < <(
  sed -une '
    /DHCPOFFER/{
      s/^\(.*\) [^ ]\+ dhcpd: DHCPOFFER on \([0-9.]\+\) to \([0-9a-f:]\+\) .*$/\3 \2 \1/p;
      q;
    }' < <(
      tail -n0 -f /var/log/syslog
))

printf "Date:\t%s\nMac:\t%s\nIP:\t%s\n" "$rDate" $rMac $rIp

要遠程停止腳本:

十五路:

mkfifo /tmp/holder
tail -f /var/log/syslog &
TailPid=$!
cat >/dev/null /tmp/holder
kill -9 $TailPid

......然后從elswhere:

echo >/tmp/holder

將終止tail命令。

鎖定的方式

#!/bin/bash

[ -e /tmp/lockfile ] && exit
echo $$ >/tmp/lockfile
[ $(</tmp/lockfile) -ne $$ ] && exit
cd /var/log
tail -f syslog &
export tPid=$!
trap "kill $tPid;rm /tmp/lockfile;exit 0" 12
wait $tPid

然后,從elswhere:

kill -USR2 $(</tmp/lockfile)

通過SSH

第二種方法通過ssh正常工作:

ssh -T root@remoteMachine /bin/bash <<"eocmd"
    [ -e /tmp/lockfile ] && exit
    echo $$ >/tmp/lockfile
    [ $(</tmp/lockfile) -ne $$ ] && exit
    cd /var/log
    tail -f syslog &
    export tPid=$!
    trap "kill $tPid;rm /tmp/lockfile;exit 0" 12
    wait $tPid
eocmd

(關心內聯腳本標簽周圍的雙引號)

然后,從elswhere:

ssh root@remoteMachine '/bin/bash -c "kill -USR2 $(</tmp/lockfile)"'

(關注報價和雙引號,按此順序)

關於安全考慮的注意事項 :這不關心安全問題! 將這種鎖文件放在/tmp可能是一個壞主意......

您可以在后台執行tail,同時將其輸出重定向到另一個文件(例如/tmp/mylog ),並在pid文件中的某處寫入進程的pid(例如~/mytail.pid ):

tail -f logfile > /tmp/mylog & echo $! > ~/mytail.pid

接下來,當你想要停止它時,只需執行:

kill `cat ~/mytail.pid`

然后,您可以看到您在此期間收集的日志內容(稍后刪除它也是個好主意):

cat /tmp/mylog
rm /tmp/mylog # just don't forget it there

您所指的帖子#2執行以下操作: 得到正確的答案

它說:

tail -f /etc/httpd/logs/access_log & # This starts tailing the file in background (see & at the end)
kill `ps | grep tail | awk '{print $1;}'` # This finds the process running above tail command and kills it

您可能需要根據需要引入sleep 100或其他內容,否則,上述兩個命令將導致拖尾只有一小部分時間才能殺死它。

根據您對@psmears的評論,您可以使用CTRL+C

@psmears - 當然。 那也行。 我想要做的就是告訴unix拖尾文件,不管我想要什么時候停止它,並在開始和結束拖尾之間得到輸出。 謝謝。 - Borat Sagdiyev 2天前

因此,您只需在ssh參數中啟動命令即可。

 ssh username@remotehost tail -f /var/log/remotelog.txt | tee result.log

當你完成后,點擊CTRL+C

在上一個命令中,我使用了tee命令,以便在終端中查看新行並將它們存儲到文件中。

如果您希望它是可編寫腳本的

您可以執行以下操作:

 ## store result to file
 FILE=result.log

 ## launch tail remote log, and store result to result.log
 ssh -n username@remote-host tail -f /path/to/remote.log > $FILE &

 ## store pid
 SSHPID=$!

 ## wait for ":q" command
 while [ "$cmd" != ":q" ]; do
    echo -ne "\n$ "
    read -n 2 cmd
 done

 ## kill ssh when you've enough
 kill $SSHPID

 ## read result
 echo "the changes made to log file were\n"
 cat $FILE

請注意,如果要分離啟動和停止腳本,則只需將SSHPID存儲在腳本中的文件中即可

 echo $SSHPID > ~/.sshpid

並從第二個檢索它

 SSHPID=`cat ~/.sshpid`

比殺死更好的是讓尾巴正確退出:

tail -n0 --pid=$(($BASHPID+1)) -F logfile | sed '/special event string in the log/q'

當管道時,PID是順序的,因此尾部的pid將是$ BASHPID,而sed的pid將是$ BASHPID + 1。 當sed命令退出時, - pid開關將導致tail退出(正確!)。 顯然這是一種基礎。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM