3

I have a python script, which can only be executed a finite number of times before it throws an error (on the terminal). I need to execute it those many times.

Is it possible to do this using a loop in a Bash script? I know a Bash script can read the output of a command from the terminal, but I have no idea how to go about this.

I tried searching for this problem on this site, but I found nothing. Sorry if this has been asked before.

EDIT

I can modify the Python script to return a value depending whether the execution is successful or not. What I want to know is, how to read this returned value in a loop in a Bash script. If the execution was successful, it must execute the Python script again. If not, it must exit.

If there is any other way, that is welcome, too.

4
  • This is too broad, if you provide more specific details then you will likely get more help. Bash can get the output of a command a number of ways, e.g. X=$(echo 'hello'), assigns hello to X. But it can also get the return value of the last command with $?, which may also be a means of capturing what you need. Commented Apr 28, 2018 at 21:06
  • You've given us very little to go on. What do you need the python script for? Did you write it yourself? What is the role of the terminal? Python scripts don't throw errors on the terminal. A python script can write messages on stderr, it can exit with an error, and it can do both. If you want to repeat to run the script until it exits with an error, you might do this: while script.py; do : ; done Commented Apr 28, 2018 at 21:26
  • What's the error from the python script? Its better to fix that first. Commented Apr 29, 2018 at 6:37
  • More details have been added to the question. Also, I do not know in advance how many times the script can be run before it fails. Commented Apr 29, 2018 at 8:40

2 Answers 2

4

Right on the command line

user@darkstar:~$ for i in `seq 1 100`; do ./run_the_python; done

Replace 100 with your max value, ./run_the_python with whatever is appropriate to launch your python script.

Update 20190625 /bin/sh: syntax error: unexpected ";"

Sign up to request clarification or add additional context in comments.

Comments

0

you can put below code in some executable file. Just replace your start script command into

"#Type start command here"

part. Script will check peer 300 second and will start if process not running. Of course you can optimize if you provide more clear information.

then call script by:

nohup ./executable.sh > Log.log &

#

#!/bin/bash


checkProcess() {
    process=$1
    if [ ${#process} != 0 ]; then
        while true ; do
             processID=$(ps -ef | grep -v grep| grep  "$process"| wc -l)
             if [ ${#processID} == 1 ]; then
                  echo "$(date) : Process is running "
             elif [ ${#processID} -gt 1 ]; then
                  echo "$(date) : Seems more than process running "
             else
                  echo "$(date) : Process is not running"
                  #Type start command here
             fi
             sleep 300s
         done
    else
        echo "You did not passed argument. Please provide process information for GREP"
    fi
} 

checkProcess scriptName

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.