0

I have this script:

#!/bin/sh
data=$(date +%Y_%m_%d_%H_%M)
#data_rolling=$(date +%Y_%m_%d_%H_%M)

rolling_restart_01=$(cat rolling_restart_01.txt)
rolling_restart_02=$(cat rolling_restart_02.txt)
rolling_restart_03=$(cat rolling_restart_03.txt)

rolling_restart_01_script=$(cat rolling_restart_01.txt)
rolling_restart_02_script=$(cat rolling_restart_02.txt)
rolling_restart_03_script=$(cat rolling_restart_03.txt)

#Restart nodo 01
if [[ ($rolling_restart_01 == "OK") || ($rolling_restart_01_script == "OK") ]]; then
    echo 'RUNNING' > rolling_restart_01.txt
    echo 'Status for node 01 is: ' >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    rolling_restart_01_script="RUNNING"
    echo $rolling_restart_01_script
    rolling_restart_01=$(cat rolling_restart_01.txt)
    echo $rolling_restart_01
fi
if [[ ($rolling_restart_01 == "RUNNING") || ($rolling_restart_01_script == "RUNNING") ]]; then
    #stato_nodi_vertica_file_01=$(cat stato_nodi_vertica_file_01.txt)
    echo 'ROLLING RESTART 01 = RUNNING' >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    if [[ $(/opt/vertica/bin/admintools -t list_allnodes | grep 'RECOVERING\|DOWN') ]]; then
        date >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        echo 'Node 01 not initialized'  >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    else 
        echo 'UP' > rolling_restart_01.txt
        echo 'Node 01 status is: ' >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        rolling_restart_01_script="UP"
        echo $rolling_restart_01_script >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        rolling_restart_01=$(cat rolling_restart_01.txt)
        echo $rolling_restart_01 >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        echo "" >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    fi
fi
if [[ ($rolling_restart_01 == "UP") || ($rolling_restart_01_script == "UP") ]]; then
    echo 'ROLLING RESTART 01 = UP' >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    echo "" >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    echo "Node 01 restart: OK " >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    /opt/vertica/bin/admintools -t list_allnodes >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    echo 'UP' > rolling_restart_01.txt
    echo 'NODE 01 STATUS: ' >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    rolling_restart_01_script="UP"
    echo $rolling_restart_01_script >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    rolling_restart_01=$(cat rolling_restart_01.txt)
    echo $rolling_restart_01 >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    echo "" >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    #Restart nodo 02
    if [[ ($rolling_restart_02 == "OK") || ($rolling_restart_02_script == "OK") ]]; then
        echo "NODE 02 RESTART" >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        echo 'RUNNING' > rolling_restart_02.txt
        echo 'Node 02 status is: ' >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        rolling_restart_02_script="RUNNING"
        echo $rolling_restart_02_script >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        rolling_restart_02=$(cat rolling_restart_02.txt)
        echo $rolling_restart_02 >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        echo "" >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
    fi
    if [[ ($rolling_restart_02 == "RUNNING") || ($rolling_restart_02_script == "RUNNING") ]]; then
        #stato_nodi_vertica_file_02=$(cat stato_nodi_vertica_file_02.txt)
        echo 'ROLLING RESTART 02 = RUNNING' >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        if [[ $(/opt/vertica/bin/admintools -t list_allnodes | grep 'RECOVERING\|DOWN') ]]; then
            date >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
            echo 'Node 02 not initialized'  >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        else 
            echo 'UP' > rolling_restart_02.txt
            echo 'Node 02 status is: ' >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
            rolling_restart_02_script="UP"
            echo $rolling_restart_02_script >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
            rolling_restart_02=$(cat rolling_restart_02.txt)
            echo $rolling_restart_02 >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
            echo "" >> /home/dbadmin/rolling_restart_vertica/log/rolling_restart_vertica_$data
        fi
    fi
fi

the 3 files contains the following results:

  • rolling_restart_01.txt > UP
  • rolling_restart_02.txt > RUNNING
  • rolling_restart_03.txt > OK

if I run the script manually it goes in this if:

if [[ ($rolling_restart_02 == "RUNNING") || ($rolling_restart_02_script == "RUNNING") ]]; then

while if I run from crontab it stops here:

if [[ ($rolling_restart_01 == "UP") || ($rolling_restart_01_script == "UP") ]]; then

Anybody knows why?

I think that the problem could be the if indenting but don't if I'm missing something.

Thanks, Emanuele

  • 1
    `#!/bin/sh` yet `[[` is part of bash. Use bash. Your script has many repetitions of the same filename. Consider using a variable, wrapping it in a function, or opening a file descriptor And check your script with shellcheck. – KamilCuk Nov 17 '22 at 12:32
  • You don't need to redirect every command. You can clean up the code a lot with things like `if cmd; then ...; fi > output_path` to redirect all commands in the ellipses (and the `cmd` that `if` is evaluating). – William Pursell Nov 17 '22 at 13:11
  • @KamilCuk I've tried all the things you suggested but still have the problem. I've also tried to export users environment variables from PATH but nothing changed. As final trial I've put also "bash" before the .sh, nothing to do. – Emanuele Sausto Nov 17 '22 at 18:02
  • 1
    `I've put also "bash" before the .sh` You have to change `#!/bin/sh` to `#!/bin/bash`, not before it. – KamilCuk Nov 17 '22 at 18:13
  • Update: the problem wasn't the if but the position of the .txt files: once I've set cd in the .sh everything works! – Emanuele Sausto Nov 17 '22 at 19:20

0 Answers0