LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-07-2022, 11:59 PM   #1
klmcguire
LQ Newbie
 
Registered: Feb 2022
Posts: 9

Rep: Reputation: 0
Waiting for multiple files to have the same string


I am running multiple files with sbatch and multiple log files are output. I want my program to wait until all of the log files have a certain string. This is what I have, but it's not quite working.

while ! grep "DONE" minimization*.log
do
sleep 2
done

The asterisk represents numbers (i.e. minimization1.log, minimization2.log, etc). I need this to check all of the log files for the string DONE and then continue when all of them have it. When I run this, it will finish and continue even if all of the log files aren't finished and don't have DONE in them.

Last edited by klmcguire; 04-08-2022 at 12:01 AM.
 
Old 04-08-2022, 12:23 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,353
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
I'm not familiar with sbatch but as for waiting for multiple log files, you can do it something like this:

Code:
for log in minimization*.log; do
      tail -F ${log} | grep -q "DONE" &
done

echo "Waiting"

wait

echo "Done waiting
If you need to catch errors from the loop or to pay attention to specific log files, then a slightly more complex approach is needed.

Edit: Or maybe the exit code from squeue -u might be of use, but as mentioned I'm not familiar with sbatch.

Last edited by Turbocapitalist; 04-08-2022 at 12:31 AM.
 
Old 04-08-2022, 12:54 AM   #3
klmcguire
LQ Newbie
 
Registered: Feb 2022
Posts: 9

Original Poster
Rep: Reputation: 0
Turbocapitalist, thanks for the reply. I tested it on 3 log files with DONE on the first line, but it just keeps waiting. What am I doing wrong? Thanks!
 
Old 04-08-2022, 01:16 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,035

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
without telling us the details I can only tell you the problem is in line 32.
 
Old 04-08-2022, 01:21 AM   #5
klmcguire
LQ Newbie
 
Registered: Feb 2022
Posts: 9

Original Poster
Rep: Reputation: 0
pan64 line 32?
 
Old 04-08-2022, 01:22 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,353
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
Yes. Very clearly. Please post the full script within [code] tags for more details.
 
Old 04-08-2022, 03:21 PM   #7
klmcguire
LQ Newbie
 
Registered: Feb 2022
Posts: 9

Original Poster
Rep: Reputation: 0
Ok, that command does what I need. However, is there a way to stop it from printing in the terminal "tail: file has appeared ... following file"? Also stop it from printing the trigger text after the log file writes it?

One more, can the tail command be killed without using Ctrl+C? Maybe in a shell script...
 
Old 04-08-2022, 04:00 PM   #8
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Simulation:

In 1st terminal
Code:
touch mylog.log
( tail -f -n0 mylog.log & ) | grep -q "DONE"
In second teminal
Code:
for i in dog cat fish cow pig DONE bird deer; do
    echo "$i" >> mylog.log
    echo "$i"
    sleep 1
done
 
Old 04-09-2022, 12:16 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,353
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
Quote:
Originally Posted by klmcguire View Post
Ok, that command does what I need. However, is there a way to stop it from printing in the terminal "tail: file has appeared ... following file"?
Yes with a redirect to /dev/null for stderr.

Quote:
Originally Posted by klmcguire View Post
Also stop it from printing the trigger text after the log file writes it?
Yes, with -q as shown above.

Quote:
Originally Posted by klmcguire View Post
One more, can the tail command be killed without using Ctrl+C? Maybe in a shell script...
That is more complicated. Untested:

Code:
#!/bin/sh

zap() {
        for p in ${pids}; do
                kill ${p}
        done
        exit
}

trap "zap" TERM INT

unset pids
for log in minimization*.log; do
        tail -F ${log} 2>/dev/null | grep -q "DONE" &
        pids="${pids} $!"
done

echo "Waiting"

wait

echo "Done waiting

trap - TERM INT

exit 0
See "man 7 signal" for more about signals.
 
  


Reply



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 capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
In if test the list contains only one specific string or multiple same specific string Mike_Brown Linux - Newbie 1 01-27-2016 10:52 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
Hard Drive Problems: timeout waiting for DMA; error waiting for DMA mintee Linux - Hardware 10 09-21-2007 05:06 AM
My browser, all day today: 'waiting for linuxquestions.org...' ..waiting.. waiting .. GrapefruiTgirl LQ Suggestions & Feedback 18 05-25-2007 05:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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