0

I'm trying to create a script on bash shell, containing 4 python scripts, I have sorted them in order to execute them sequentially, how do I make a condition if anyone of them not executed or returned any error then stop the shell

#!/bin/bash -x cd /home/hadoop/Traffic_Stat_Process python3 Table_Dropper.py /usr/bin/spark-submit --jars /home/hadoop/vertica/lib/vertica-jdbc-9.1.1-0.jar Spark_JDBC_Connector.py hadoop fs -rm -r /hadoopData/hive/warehouse/temp python3 2th_phase.py python3 3th_phase.py 
4
  • if ! python3 arg; then echo "python3 arg failed" >&2; exit 1; fi. Commented Apr 2, 2019 at 13:56
  • but the 2th script executed by spark-submit not python3 Commented Apr 2, 2019 at 14:03
  • if ! /usr/bin/spark-submit --jars /home/hadoop/vertica/lib/vertica-jdbc-9.1.1-0.jar Spark_JDBC_Connector.py ; then echo "the spark-submit failed" >&2; exit ; fi Commented Apr 2, 2019 at 14:06
  • Put || exit after any command you want to terminate the script on exit; there's also set -e, but there are compelling reasons to avoid it. Nothing about this is specific to Python, so the title can probably be focused a bit. Commented Apr 2, 2019 at 14:27

1 Answer 1

1

Putting && between commands will tell bash to execute commands from start to end, and if any fail along the way, it will stop executing.

For example:

echo $(non_existent_variable) && cd ~ 

cd ~ is a valid command. However, since non_existent_variable is not defined, the first command fails and an error is thrown immediately.

In your case, you can add && \ at the end of each of your lines.

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

1 Comment

In How to Answer, note the section "Answer Well-Asked Questions", and therein the bullet point regarding questions which "have already been asked and answered many times before".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.