Ken Hall
Ken Hall

Reputation: 21

Can anyone explain what is going wrong with this simple bash script?

Below I have a simple bash script that I basically want to run for X number of seconds and then restart itself. The problem I am having and to be honest somewhat confused about is that I am defining $SECONDS in the script and never redefine or manipulate the value. Yet, if you run the script as is with -x you can see that $SECONDS is incrementing along with $loopcounter. WTH?!?

#!/bin/bash -x

SECONDS=30
loopcounter=0
LOGFILE="/var/log/somelogfile"

while [ /bin/true ]
do
    eval "tail -f $LOGFILE | grep -i error &"
    while [ $loopcounter -lt $SECONDS ]
    do
        loopcounter=$(($loopcounter + 1))
        sleep 1
    done
    echo "Restarting.."
    pkill -f "tail -f $LOGFILE"
done

Upvotes: 1

Views: 35

Answers (1)

choroba
choroba

Reputation: 241858

SECONDS is a special variable. See man bash:

Each time this parameter is referenced, the number of seconds since shell invocation is returned. If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. If SECONDS is unset, it loses its special properties, even if it is subsequently reset.

Don't use upper case variable names if you don't want to clash with the special ones.

Upvotes: 6

Related Questions