LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-11-2020, 05:16 PM   #1
gregors
Member
 
Registered: Mar 2018
Posts: 130

Rep: Reputation: Disabled
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
 
Old 09-11-2020, 06:40 PM   #2
berndbausch
LQ Guru
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 5,325

Rep: Reputation: 1603Reputation: 1603Reputation: 1603Reputation: 1603Reputation: 1603Reputation: 1603Reputation: 1603Reputation: 1603Reputation: 1603Reputation: 1603Reputation: 1603
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.
 
1 members found this post helpful.
Old 09-11-2020, 06:56 PM   #3
gregors
Member
 
Registered: Mar 2018
Posts: 130

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
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
 
Old 09-12-2020, 09:10 AM   #4
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 575

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367

This does not do what you want:
Quote:
Originally Posted by gregors View Post
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.)

 
1 members found this post helpful.
Old 09-12-2020, 09:32 AM   #5
gregors
Member
 
Registered: Mar 2018
Posts: 130

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
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 ...

Last edited by gregors; 09-12-2020 at 10:15 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to log internal-sftp chroot jailed users access log to /var/log/sftp.log file LittleMaster Linux - Server 0 09-04-2018 03:45 PM
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
parsing a text file - to awk or not to awk ? rollyah Programming 9 08-18-2011 02:20 PM
I download then wait and download and wait... jsheffie SUSE / openSUSE 1 11-04-2005 04:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration