Search code examples
bashif-statementtimertiming

Continuous If statement


I have 2 scripts one is software.sh and the other timer.sh both get executed in separate terminals. I want the if statement that checks for software execution duration to be checking continuously but apparently it only gets checked once at start of execution :(

This is the timer.sh which gets the PID of software.sh and counts its running time

#!/bin/bash
while ((1)); do
PID_Ardu=$(pidof arducopter)
Time= ps -p $PID_Ardu -oetime= | tr '-' ':' | awk -F: '{ total=0; m=1; } { for (i=0; i < NF; i++) {total += $(NF-i)*m; m *= i >= 2 ? 24 : 60 }} {print total}'
if [[ $Time < 20 ]]; then **#THIS SHOULD BE EXECUTED CONTINOUSLY**
        echo $Time
else 
        echo INSIDE
        ps -ef | grep software| grep -v grep | awk '{print $2}' | xargs kill
fi
done    

This is software.sh which executes various generated files

#!/bin/bash
chmod a+x *.exp


./ACCF01.exp | wait |echo "FINISHED SCRIPT 1" #Run the ACCF01 script

sed -i '/ACCF01.exp/d' ./software.sh #Delete previous line after finishing execution.

./ACCF02.exp | wait |echo "FINISHED SCRIPT 2"

sed -i '/ACCF02.exp/d' ./software.sh

./ACCF03.exp | wait |echo "FINISHED SCRIPT 3"

sed -i '/ACCF03.exp/d' ./software.sh

./ACCF04.exp | wait |echo "FINISHED SCRIPT 4"

Solution

  • The Time variable is assigned a string, not the return value of the pipeline, because it needs to enclose it in $(..) or backticks.

    In any case the If is executed multiple times, it just has no changing numerical value in the variable.

    When you try to debug loops and variable assignments you can use set -x or start with bash -x time.sh in the future. It will print out all lines execute and variables expanded.