LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Wait for text in log file (awk anyone?) (https://www.linuxquestions.org/questions/linux-software-2/wait-for-text-in-log-file-awk-anyone-4175681965/)

gregors 09-11-2020 05:16 PM

Wait for text in log file (awk anyone?)
 
Hi there!

I want to write me a backup script. The overall procedure I have in mind is:

- log in as root
- start script
- start backup disk (external 4 TB USB drive)
- wait until backup finishes

So what I see in /var/log/messages when the backup disk is plugged in is something like this:

Code:

Sep 11 01:47:06 mimi kernel: [50974.093519] sd 7:0:0:0: [sdh] Spinning up disk...
Sep 11 01:47:06 mimi mtp-probe: checking bus 3, device 3: "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-6/3-6.1"
Sep 11 01:47:06 mimi mtp-probe: bus: 3, device: 3 was not an MTP device
Sep 11 01:47:06 mimi mtp-probe: checking bus 3, device 3: "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-6/3-6.1"
Sep 11 01:47:06 mimi mtp-probe: bus: 3, device: 3 was not an MTP device
Sep 11 01:47:16 mimi kernel: [50975.123016] .........ready

How can I make my script wait until the two bold texts appeared? I remember that I did something like this with awk, but that's long ago (~20 years) and I don't have a backup of that script.

So is there anyone who can help me with this?

Ideally the result would run on any Unix and would use old, reliable and widely available tools.

TIA

Gregor

berndbausch 09-11-2020 06:40 PM

You could use awk, e.g.
Code:

tail -f /var/log/messages | awk '/.....ready/ { exit; }'
but normally any attempts to access the disk should just wait until it's ready. I speak from experience, since my backup server does exactly that.

gregors 09-11-2020 06:56 PM

Quote:

Originally Posted by berndbausch (Post 6164965)
You could use awk, e.g.
Code:

tail -f /var/log/messages | awk '/.....ready/ { exit; }'
but normally any attempts to access the disk should just wait until it's ready. I speak from experience, since my backup server does exactly that.

Great! Works!

Just for the record - my very quick-and-dirty script:
Code:

#!/bin/bash

# Auf Meldungen in /var/log/messages warten
tail -n 1 -f /var/log/messages | awk '/Spinning up disk.../ { exit; }'
echo -n "Platte wurde angeschlossen, warte auf Bereitschaft ..."

tail -n 1 -f /var/log/messages | awk '/.....ready/ { exit; }'
echo " bereit."

# We're testing, so ...
exit

mount /dev/sdh1 /media/backup
umount /home/gszaktilla/dokumente/datensammlung/video
umount /home/gszaktilla/dokumente/datensammlung

echo -n "Kopiere /home/gszaktilla ... "

if [ -e /media/backup/home/gszaktilla ]; then
  cp -ar /home/gszaktilla/* /media/backup/home/gszaktilla/
fi

echo "fertig."

mount /home/gszaktilla/dokumente/datensammlung
mount /home/gszaktilla/dokumente/datensammlung/video

echo -n "Gleiche /home/gszaktilla/dokumente/datensammlung ab ... "

if [ -e /media/backup/datensammlung ]; then
  rsync --exclude=lost+found/ -auqEhs \
  /home/gszaktilla/dokumente/datensammlung/* \
  /media/backup/datensammlung/
fi

echo "fertig."

umount /dev/sdh1

echo "--- Backup beendet ---"
echo

# eof

Gregor

boughtonp 09-12-2020 09:10 AM


 
This does not do what you want:
Quote:

Originally Posted by gregors (Post 6164970)
Code:

tail -n 1 -f /var/log/messages | awk '/.....ready/ { exit; }'

Awk will incorrectly exit if it sees text like "device is not ready" (or similar), because you're using a regex so the "." matches any character (and you haven't indicated start or end bounds)

You could replace the regex with either "/ \.\.\.\.\.\.\.\.\.ready$/" or simply "/ \.{9}ready$/", but an even better option would be to do a plaintext match on the last field of the line ($NF) with exactly what it contains, i.e:

Code:

tail -n 1 -f /var/log/messages | awk '$NF == ".........ready" { exit; }'
(And depending on what might log to that file, I'd consider using a more distinct/meaningful token.)


gregors 09-12-2020 09:32 AM

Quote:

Originally Posted by boughtonp (Post 6165110)
Awk will incorrectly exit if

Thanks a lot for this addition!

Gregor

PS: Having implemented (well, copy+pasted) what you wrote I looked up 'NF' in the awk man page and I am irritated by '==' (are you comparing a number and a string?!) and decided I should take a closer look at awk. Once there was an O'Reilly book on awk an sed ...


All times are GMT -5. The time now is 07:03 AM.