-1

I have this bash script that runs 2 python scripts in parallel, they bring data from another server via API, they work fine, but sometimes errors ocurrs when connection problems exists, and only fraction of the data is delivered

I want to make shure that if an error ocurrs the script start again, so I haved tried using until, but when I on purpose disconnect the internet to cause an error the script exits and doesnt start again.

can you help me with this?

!/bin/bash

scripts=/root/scripts
datos=/root/datos
fecha=$(date +"%Y_%m_%d")

until $scripts/filesystem.py > $datos/filesystem_$fecha.log &
do
    echo error, retrying en 10 seconds
    sleep 10
done

until $scripts/cpu.py > $datos/cpu_$fecha.log &   
do
    echo error, retrying en 10 seconds
    sleep 10
done
wait

1 Answers1

1

I haven't seen your python code, but it seems to me that your python scripts are not notifying the shell about the failure in program execution.

You need to handle the failure in the python and return a proper exit code the shell can work with. A code different than 0 will be interpreted as an error by the shell.

You can do it by a call to sys.exit like this:

import sys

print('Simulating failure')

sys.exit(1)

Another way: if you manage to handle the error in python perhaps you can retry in python code and no shell loop needed, nor sys.exit call. It's up to you.

Ictus
  • 121
  • 4