How to end tail -f from the script?

Hi,

i have a script

tail -f logs| grep item

i need to end the script once it finds item in the logs folder, this happens on run time.

If you want to stop the execution of the script when a certain "item" is logged to logs file, instead of using tail , you could try this:

while true; do
    grep -q item logs && echo "Event \"item\" occurred"; exit 0
    sleep 5
done

Chacko ,

Thanks for the solution

it din't worked, but i found the alternate for the same

tail -f logs | awk '/item/;/item/ { exit }'

Sorry my mistake, it should have been,

grep -q item logs && (echo "Event \"item\" occurred"; exit 0)

Hello,

Could you explain what does the ; do in awk?

I know that between / / is to find an item.

Thanks.

It separates two independent statements.

The first /item/, by itself, means 'print lines matching "item"'. It defaults to print when no other statement is given. The second one, which has a code block after, means 'quit when a line matches "item"'.

1 Like

It looks similar to ,

It's not. It's effectively the same thing as a newline.

I've tried but I can't still figure it out..

kibou@laptop:~$ cat text
first line
second line
third line
fourth line
kibou@laptop:~$ awk '/first/;/fourth/ {exit}' text 
first line
kibou@laptop:~$ awk '/second/;/fourth/ {exit}' text 
second line
kibou@laptop:~$ awk '/second/;/third/ {exit}' text 
second line
kibou@laptop:~$ awk '/second/;/fifth/ {exit}' text 
second line
kibou@laptop:~$ awk '/fourth/;/fifth/ {exit}' text 
fourth line
kibou@laptop:~$ awk '/fourth/;/second/ {exit}' text 
kibou@laptop:~$ 

Clearer now:

         pattern        action
awk     '/first/        # default: print
         /fourth/       {exit}          ' text
awk     '/second/       # default: print
         /fourth/       {exit}          ' text 
awk     '/second/       # default: print
         /third/        {exit}          ' text 
awk     '/second/       # default: print
         /fifth/        {exit}          ' text
awk     '/fourth/       # default: print
         /fifth/        {exit}          ' text

?

1 Like

Yes!!

Thank you, RudiC.