For loop or while loop from a text file

Hi all,

i developed a script to measure the uptime of a process in a Solaris 10/11 environments.
All is well, but i came across a situation where there are multiple processes of the same name. Basically i have the following result file:

beVWARS 13357 19592122
beVWARS 14329 19591910
beVWARS 6371 20436149
beVWARS 6368 20436149
beVWARS 6374 20436149
beVWARS 13361 19592118
beVWARS 13988 19591989
beVWARS 6367 20436149
beVWARS 6365 20436149
beVWARS 6355 20436149
beVWARS 6363 20436149
beVWARS 14055 19591982
beVWARS 13267 19592147
beVWARS 13562 19592097
beVWARS 6361 20436149
beVWARS 14228 19591944

Where first column is the process name, followed by it's pid and the uptime in seconds.

i am trying to find a way to get the uptime vs. an if condition (by a loop?) where if the uptime is less then say 3600 seconds it outputs the process and it's pid on the same line.

Bascially i tried:

while read line; do
if [[ $line -lt $CRITICAL_SIZE ]]; then
echo -n `grep $line $CRONOUTFILE | awk '{print$2,$3}'`
fi
done <<< "$TIME"

Where $TIME contains only the 3rd column, i.e. the uptime, but it's not working as it should.
$CRONOUTFILE is the output file as shown above.
What i am trying to achieve is to output only the process and pid in a similar output:

Critical - the following processes $PROCESS are less then $CRITICAL_SIZE - $PID:$TIME, $PID:$TIME...,$PID:$TIME

Where the $PROCESS can be easily set an argument in the script.

I am totally confused in using the for loops or the while loop. May you kindly give me some hints?

Rgds,

Matthew

PROCESS=beVWARS
CRITICAL_SIZE=3600
CRONOUTFILE=cronoutfile

echo "Critical - the following processes $PROCESS are less then $CRITICAL_SIZE - $(awk '$1== proc && $3 < cs {printf (!c++ ? "" : ",") $2 ":" $3}' proc=$PROCESS cs=$CRITICAL_SIZE $CRONOUTFILE)"
1 Like

Hi,

Thanks. i understood the flow but can't understand where the error is:

awk: syntax error near line 1
awk: illegal statement near line 1

i added a "}" in

echo "Critical - the following processes $PROCESS are less then $CRITICAL_SIZE - $(awk '{$1== proc && $3 < cs {printf (!c++ ? "" : ",") $2 ":" $3}' proc=$PROCESS cs=$CRITICAL_SIZE $CRONOUTFILE)"

But still got the same error

If on Solaris, use either nawk or /usr/xpg4/bin/awk

1 Like

Thanks, both nawk and /usr/xpg4/bin/awk worked