0

I have a script that tails a log file, and then uploads the line. I would like to have it exit as soon as the first line is read:

#!/bin/bash

tail -n0 -F "$1" | while read LINE; do
  (echo "$LINE" | grep -e "$3") && curl -X POST --silent --data-urlencode \
    "payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "$2";
done
7
  • Exit as soon as the first line is read? Or when the first data is uploaded? Commented Jul 17, 2017 at 22:43
  • As soon as the first line is read and sent off via curl. Commented Jul 17, 2017 at 22:43
  • What if the first line read doesn't match what's passed in as the third arg? Commented Jul 17, 2017 at 22:44
  • Right now it uploads on each line added that contains the third arg of the script ($3) Commented Jul 17, 2017 at 22:45
  • The third arg is the url to which it writes. ex: bash log-tail.sh log_4 localhost:5000/utils/log/4 Commented Jul 17, 2017 at 22:47

2 Answers 2

1

If you want to exit as soon as the first line is uploaded you can just add a break:

#!/bin/bash

tail -n0 -F "$1" | while read LINE; do
  (echo "$LINE" | grep -e "$3") && curl -X POST --silent --data-urlencode \
    "payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "$2" && break;
done
Sign up to request clarification or add additional context in comments.

Comments

1

The issue was the tail command wasn't getting killed. A slightly modified version of my script (I didn't end up needing the echo to stdout)

#!/bin/bash

tail -n0 -F "$1" | while read LINE; do
    curl -X POST --data-urlencode "payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "$2" && pkill -P $$ tail
done

This answer helped as well: https://superuser.com/questions/270529/monitoring-a-file-until-a-string-is-found

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.