.Bat file to run multiple .Py scripts

16826
9
Jump to solution
04-19-2017 08:01 AM
jaykapalczynski
Frequent Contributor

I am looking for info on how to properly write a .bat file that will run multiple python scripts sequentially.

If one of those scripts fails then I need the process to stop.

BUT i need then to run in sequence and wait before starting the next.

I think this is close but nothing in there that aborts if fails.

@echo off
start /wait D://PythonScripts/Script1
start /wait D://PythonScripts/Script2
start /wait D://PythonScripts/Script3
start /wait D://PythonScripts/Script4

0 Kudos
1 Solution

Accepted Solutions
MichaelVolz
Esteemed Contributor

I create a list to store the errors in near beginning of python script

ErrList = []

In the except portion of your python scripts (trapping the error) you need to put in the following code to trap the error messages and write them to a log file:

except Exception as e:

   ErrList.append(e.message)

   FailLog = open("path to log file"/FailLogFile, 'w');

   FailLog.write("The following error(s) has been encountered:")

   FailLog.write(ErrList[0])

   FailLog.close()

In the bat file I check for existence of a FailLog file by name as I rename a previous fail log at the beginning of the bat file to ensure no FailLog file by that specific name exists at the start of the calls to the python scripts.

if exist FailLog (goto :BAIL)

You could probably simplify this part and just have if exist FailLog (goto :EXIT)

:EXIT

View solution in original post

0 Kudos
9 Replies
JonathanQuinn
Esri Frequent Contributor

Looks like you can do something with sys.exit() or exit() to return a failure to the command prompt, (exit code 1), and then add some logic within the .bat to check the exit code..

0 Kudos
curtvprice
MVP Esteemed Contributor

Bat can handle this using shell conditional execution, without clumsy if statements on %errorlevel%.

&& means to execute the next command as long as there is no error returned from the previously run tool. PS has a similar operator "-and"

@echo off 
pushd D:\PythonScripts
Script1.py && Script2.py && Script3.py && Script4.py‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Batch files - Conditional execution 

syntax - Can I get && to work in Powershell? - Stack Overflow 

This requires that your python script will return error condition to the shell, this will happen if python fails with an error dump, but if you are trapping with try/except you will have to leave your script with sys.exit(1) to return an error condition to the shell (sys.exit() won't do it, you need to supply the 1).

MichaelVolz
Esteemed Contributor

In my organization's bat file, we check for the existence of a fail log that is generated in any of the python scripts that are part of the bat file.  If the fail log does exist (It is renamed at the beginning of each run) at any point between python calls, then the bat file exits gracefully without running the remainder of the python scripts.

So you would need to write to an error log in your python code and then check for this file in your bat file

if exist fail.log (goto :BAIL) else echo fail.log not found

:BAIL

goto :EXIT

:EXIT

0 Kudos
jaykapalczynski
Frequent Contributor

If I run as below will this work?  Just wont have the fail safe of whether or not one of the scripts failed?

@echo off
start /wait D://PythonScripts/Script1
start /wait D://PythonScripts/Script2
start /wait D://PythonScripts/Script3
start /wait D://PythonScripts/Script4

0 Kudos
MichaelVolz
Esteemed Contributor

With your current bat file, there is no way for it to stop running a successive python script if it fails on the current python script because it will just exit the current python script with an error and move on to the next python script.  You need to explicitly tell the bat file to not run successive python scripts if the current script fails and that is what the fail log is for.

Now if python scripts are not dependent on each other for working properly, then your current setup could work.

In my scenario, the python scripts are dependent on each other, so if 1 python script fails I don't want to run the successive scripts.

0 Kudos
jaykapalczynski
Frequent Contributor

Yea that part I understand...just want to confirm...just gonna give this a test...and add the Fail safe another day.

Thanks for your thoughts.

Any examples of the code to do the testing?  Creating the log file and then testing if it exists?  I assume I do this in the python script and not in the Bat file.

0 Kudos
MichaelVolz
Esteemed Contributor

I create a list to store the errors in near beginning of python script

ErrList = []

In the except portion of your python scripts (trapping the error) you need to put in the following code to trap the error messages and write them to a log file:

except Exception as e:

   ErrList.append(e.message)

   FailLog = open("path to log file"/FailLogFile, 'w');

   FailLog.write("The following error(s) has been encountered:")

   FailLog.write(ErrList[0])

   FailLog.close()

In the bat file I check for existence of a FailLog file by name as I rename a previous fail log at the beginning of the bat file to ensure no FailLog file by that specific name exists at the start of the calls to the python scripts.

if exist FailLog (goto :BAIL)

You could probably simplify this part and just have if exist FailLog (goto :EXIT)

:EXIT

0 Kudos
Richidt_Corey
New Contributor II

Hello,

I am looking to do something very similar here. I have 6 individual scripts that I would like to have a .bat file kick off. However, my current .bat file is just executing the first script and not the others. The scripts are NOT dependent on each other, but it would be nice to record any script that fails.

Here is what I have in my .bat file:

Richidt_Corey_0-1647900001476.png

 

0 Kudos
Richidt_Corey
New Contributor II

Hi there, jaykapalczynski any thoughts on this? Wondering if you found a solution that worked for your original post? Thanks!

Tags (1)
0 Kudos