Waiting for multiple files to have the same string
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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!
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...
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
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.