Skip to content
Advertisement

Strange output in shell script

I’m encountering a very strange thing while writing a shell script.

Initially I have the following script.

OUTPUT_FOLDER=/home/user/output
HOST_FILE=/home/user/host.txt
LOG_DIR=/home/user/log
SLEEPTIME=60

mkdir -p $OUTPUT_FOLDER

while true
do
    rm -f $OUTPUT_FOLDER/*
    DATE=`date +"%Y%m%d"`
    DATETIME=`date +"%Y%m%d_%H%M%S"`
    pssh -h $HOST_FILE -o $OUTPUT_FOLDER
    while read host
    do
        numline=0
        while read line
        do
            if [[ $numline == 0 ]]
            then
                FIELD1=`echo $line | cut -d':' -f2 | cut =d' ' -f2`
            fi
            ((numline+=1))
        done < ${OUTPUT_FOLDER}/${host}
        echo "$DATETIME,$FIELD1" >> ${LOG_DIR}/${DATE}.log
    done < $HOST_FILE
    sleep $SLEEPTIME
done

When I ran this script, every 60 seconds, I’ll see the $DATETIME,$FIELD1 values in my log file.

What is very strange, is that every 30 seconds or so, after the first minute has passed, I’ll see 20160920_120232,, meaning there was output where there shouldn’t be, and I guess because I deleted the contents of my output folder, FIELD1 is empty.

What is even more strange, is while trying to debug this, I added a few more echo statements to print to my log file, and then I deleted those lines. However, they continued to be printed every 30 seconds or so, after the first minute has passed.

What is stranger still is that I then commented out everything inside the while true block, that is,

OUTPUT_FOLDER=/home/user/output
HOST_FILE=/home/user/host.txt
LOG_DIR=/home/user/log
SLEEPTIME=60

mkdir -p $OUTPUT_FOLDER

while true
do
: << 'END'
    rm -f $OUTPUT_FOLDER/*
    DATE=`date +"%Y%m%d"`
    DATETIME=`date +"%Y%m%d_%H%M%S"`
    pssh -h $HOST_FILE -o $OUTPUT_FOLDER
    while read host
    do
        numline=0
        while read line
        do
            if [[ $numline == 0 ]]
            then
                FIELD1=`echo $line | cut -d':' -f2 | cut =d' ' -f2`
            fi
            ((numline+=1))
        done < ${OUTPUT_FOLDER}/${host}
        echo "$DATETIME,$FIELD1" >> ${LOG_DIR}/${DATE}.log
    done < $HOST_FILE
    sleep $SLEEPTIME
END
done

Even with this script, where I’m expecting nothing to be printed to my log file, I see my previous echo statements that I have deleted, and the lines with the empty FIELD1. I have checked that I’m running the correct version of the script each time.

What is going on?

Advertisement

Answer

I tried to reproduce it and couldn’t. Thre is no reason for this script (when commented as you have) to produce any output.

There are couple of cases:

  1. As others pointed out, there may be some other script writing to your log file
  2. You may have left few instances of this script running on other terminals
  3. You may be running this script from crontab once every 5 mins

In order to rule out possibilities, do a “ps -aef” and check out all the processes that could resemble the name of your script. To catch things from crontab, you may have to watch the output of “ps -aef” a big longer (or just check in the crontab entries).

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement