0

First of all, I don't know how to search what I want to do.

I have one exec that produces outputs in a terminal (Linux). Let's take a simple C program a.out:

#include <stdio.h>
int main (int argc, char *argv[]) {
int i=0;
float j=0;
for(i=0; i<=10000000;i++)
  {
    j = i*-1e-5;
    printf (" %d 2.0 %f 4.0 5.0\n",i,j);
  }
}

Outputs produced are like :

 0 2.0 -0.000000 4.0 5.0
 1 2.0 -0.000010 4.0 5.0
 2 2.0 -0.000020 4.0 5.0
 3 2.0 -0.000030 4.0 5.0
 ...

Depending on this outputs I want to :

  1. Launch this exec
  2. "Capture" outputs
  3. If 3rd column value reach -0.5, stop/kill exec

How will you do this ?

For instance, exec is not stopped with this script exec.sh:

#/bin/sh
PROG=./a.out
$PROG > output  &
progpid=$!

(tail -fn 0 output & echo $! > tailpid ) | awk -v progpid=$progpid '{
    if($3<=-0.5){
      system("kill "progpid)
      # system( ##update other file )
      system("kill $(<tailpid)")
    }
 }'

Any ideas ?

Thanks in advance

Thomas P.
  • 31
  • 5
  • 1
    Points 1-3 are similar to [this question](http://stackoverflow.com/q/14899310/828193). For point 4 you could probably use `sed` to change the file, and for point 5 have the whole thing in a bash loop. Not sure if this question is specific enough to be answered as it is... – user000001 May 24 '13 at 15:28
  • Do you want to monitor a running executable's output and take different actions on each, or do this for each line in a file containing the output, or somehow treat the entire collected output? Your question is not very clear. – tripleee May 24 '13 at 15:31
  • how do you "stop exec"? – glenn jackman May 24 '13 at 18:24

1 Answers1

1

I think this sort of construct addresses all of your points:

programname > output &
progpid=$!
(tail -fn 0 output & echo $! > tailpid ) | awk -v progpid=$progpid '{ 
    if( condition ) { 
        system("kill "progpid)
        system( ##update other file )
        system("kill $(<tailpid)")
    }
}'

We run the program in the background and redirect output to output. Then we monitor output as it is updated using the tail -f option, which reads lines from the end of the file as they are added. Then we pipe this into awk, which can run a system command to kill the program process if condition is met, then run another command to update your parameters in your separate text file, then run another command to kill tail so that it doesn't hang in the background forever (awk will also exit once tail is killed).

qwwqwwq
  • 6,999
  • 2
  • 26
  • 49
  • I'd be curious if buffering will allow the controlled process to run ahead before the stop condition is detected and it is killed. Though the question doesn't specify if letting it continue will have undesired side effects. – Chris Stratton May 24 '13 at 18:55
  • I was thinking the same thing, very likely it will happen, but at the end of the day if you really need that fine level of control you're going to need to write it in into your program! – qwwqwwq May 24 '13 at 19:03
  • @chepner - if you want to suggest that the -9 argument is not advisable that would be appropriate as a comment, but it's not appropriate to just edit it out of someone else's answer. – Chris Stratton May 24 '13 at 19:08
  • `kill -9` is absolutely unnecessary for ordinary usage. If your program isn't responding to TERM signal (ok, you might need an interrupt), there is something seriously wrong with the program that is far beyond the scope of this question. – chepner May 24 '13 at 19:10
  • I didn't mind the edit for the record, kill -9 was indeed unnecessary, removed it here – qwwqwwq May 24 '13 at 19:59