LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-07-2012, 08:22 AM   #1
littleplane
LQ Newbie
 
Registered: Mar 2012
Posts: 17

Rep: Reputation: Disabled
Filtering the output of a process


Hi,

I'd like to write a bash script what launches a program with a lot of output (this program doesn't stop unless you kill it, but it runs like a daemon). So I need to filter its output in runtime and kill it if this output contains a specific string. Unfortunatelly

./dameon_program | grep -Ei "string"

doesn't a good solution because it cannot capture this string.
Any idea how can I solve this?
 
Old 07-07-2012, 08:59 AM   #2
littleplane
LQ Newbie
 
Registered: Mar 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Okay, the fist part is solved:
./daemon_program 2>&1 | grep -Eio "LOADFAIL"

This command captures the proper string but how can I kill the program if I detect this?
I tried the following:

if [ `./daemon_profram 2>&1 | grep -Eio "LOADFAIL"` != "" ]; then
kill `pidof deamon_program`
fi

But it doesn't work. Ideas?
 
Old 07-07-2012, 10:02 AM   #3
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,928

Rep: Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305
grep will wait until your daemon_program ends, so you need to tell grep to stop at the first match.
you do not need -E and -o to grep, but you would need -L. Try:
./daemon_program 2>&1 | grep -Li "LOADFAIL" && pkill daemon_program
 
Old 07-10-2012, 03:50 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
You probably need to rethink the problem.


something like this could do:

Code:
SEARCH=profile

find $HOME | while read x;do

    echo $x
    [ "${x#*$SEARCH*}" ] || break # do kill here

done

# or do kill here
obviously replace the find with your daemon
 
Old 07-10-2012, 09:39 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
bigearsbilly has posted a superior solution, which allows the pipeline to accumulate very little data by breaking the output of the child process into individual lines. I think the other solutions will accumulate the child process output in a buffer until an EOF is received (the child terminates), and then the grep will receive the full complement of buffered data.

--- rod.
 
Old 07-10-2012, 12:20 PM   #6
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,928

Rep: Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305
Quote:
Originally Posted by theNbomr View Post
bigearsbilly I think the other solutions will accumulate the child process output in a buffer until an EOF is received (the child terminates), and then the grep will receive the full complement of buffered data.
--- rod.
I made a "slow" cat:
Code:
while read a
do
    echo $a
    # this line for debug only
    echo $a > /dev/tty
    sleep 1
done < searchfile | grep -L 'pattern'
without -L it will run until the end of searchfile - regardless of the pattern, but with -L it will be stopped after the first match, grep does not wait "any longer". After the match even the while loop will be finished because the pipe has been broken. (Actually the while loop will print out a few additional debug line by then)
 
Old 07-10-2012, 10:20 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
pan64 - is -L the correct solution? From the man page:
Quote:
-L, --files-without-match
Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.
I would suggest either -l (that is lower case 'el') or -m1 for first match
 
Old 07-11-2012, 02:08 AM   #8
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,928

Rep: Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305
I think they will differ only how grep will exit (what will be the exit code). All of them will stop on the first match.
 
Old 07-11-2012, 03:00 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
Quote:
Originally Posted by theNbomr View Post
bigearsbilly has posted a superior solution,
I couldn't agree more
 
  


Reply

Tags
bash, output, process


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
Sending Signal from Child Process to Parent Process : Not getting desired output thelink123 Linux - General 4 10-26-2012 09:05 PM
[SOLVED] Filtering out duplicate lines from a find/grep output thundervolt Linux - Newbie 10 03-25-2010 03:32 AM
want to see process output freakolowsky Linux - General 12 06-01-2007 06:54 PM
Getting AND filtering the output of "wvdial" rkrijger Linux - Software 0 11-17-2003 08:02 AM
Need help filtering out threads in ps command output eDubster Linux - General 4 04-15-2003 04:09 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration