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 :
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
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.