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? |
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? |
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 |
You probably need to rethink the problem.
something like this could do: Code:
SEARCH=profile |
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. |
Quote:
Code:
while read a |
pan64 - is -L the correct solution? From the man page:
Quote:
|
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.
|
Quote:
|
All times are GMT -5. The time now is 07:45 AM. |