3

I am trying to monitor the changes of a file via SSH and copy the changes of it in local computer using BASH. I ran the following command to monitor and copy:

ssh root@192.168.88.238 "tail -F /media/sdcard/SA000001.log"> ~/Desktop/Modem.log

Which works perfectly. But then it keeps running. I want to terminate the tail and the ssh after certain period (Or when a certain string is found).

I tried the following command, but it didn't work:

ssh -t root@192.168.88.238 "tail -F /media/sdcard/SA000001.log && sleep 10 && exit"> ~/Desktop/Modem.log

I also tried passing exit and the ASCII value of CTRL+C via echo, but was unable to end/kill the tail and ssh.

Then I tried the following:

#!/bin/sh 

ssh -t root@192.168.88.238 "tail -F /media/sdcard/SA000001.log | while read TEMPLOGLINE"> ~/Desktop/Modem.log

do
   [[ "${TEMPLOGLINE}" == *"temp"* ]] && pkill -P $$ tail
done

exit

I have also tried this:

#!/bin/sh 

ssh -t root@192.168.88.238 "tail -F /media/sdcard/SA000001.log"> ~/Desktop/Modem.log

ssh -t root@192.168.88.238 "/X03"
ssh -t root@192.168.88.238 "exit"

Here is my bash file at this moment:

#!/bin/sh 

ssh -t root@192.168.88.238 "tail -F /media/sdcard/SA000001.log"> ~/Desktop/Modem.log

Any suggestions on how I should proceed with this?

Thanks in advance.

4

It sounds like you're looking for something like this:

#!/bin/bash
ssh root@192.168.88.238 tail -F /media/sdcard/SA000001.log | while read line; do
    echo "$line"
    if [[ $line =~ 'temp' ]]; then
        exit
    fi
done

Save the script above as foo.sh or whatever, make it executable (chmod a+x foo.sh) and then run it:

foo.sh > ~/Desktop/Modem.log
4

This is the command you need to do the job:

ssh USER@HOST "bash -c 'sed '/PATTERN/q' <(tail -F /path/to/dest/log)'"> inlocal.log

you can use awk instead as following as well:

ssh USER@HOST "bash -c 'awk '1;/PATTERN/{exit}' <(tail -F /path/to/dest/log)'"> inlocal.log
  • That does work, but has the (possible) drawback that it won't show the file grow in "real time" because sed buffers its output so lines will be printed in bunches. – terdon Aug 18 '17 at 12:01
  • @terdon I have tested 100% successfully my answer! but buffering Yes it does – αғsнιη Aug 18 '17 at 12:02
  • Yes, and it absolutely works and I have upvoted it. All I am saying is that it won't show the results in "real time" because sed buffers its output. This means it will take a few seconds for the first output to appear. I don't know if this is a problem for the OP (won't be if they just want everything saved in a file) but I felt it was worth mentioning. – terdon Aug 18 '17 at 12:04
  • What is the point of sed '/PATTERN/q' <( tail -F logfile ) here instead of tail -F logfile | sed '/PATTERN/q? – shadowtalker Aug 18 '17 at 16:16
  • please refer to this Q&A – αғsнιη Aug 18 '17 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.