1

I have 2 python scripts that do some geoprocessing, one is depending on the other and i run them via a batch file. At the end of the execution, i send email using powershell script for feedback. I just want to receive emails when there is an error and not everytime the script is running. I want a way to test if the batch file has run successfuly the python scripts.

Any Ideas?

Compo
  • 36,585
  • 5
  • 27
  • 39
Mar
  • 419
  • 1
  • 7
  • 19
  • 1
    Have a look at [try-except](https://www.w3schools.com/python/python_try_except.asp) – Claudio P Dec 16 '19 at 12:04
  • 1
    you can use `exit(1)` to exit script with error code `1` and cmd can get it [How do I get the application exit code from a Windows command line?](https://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-command-line) – furas Dec 16 '19 at 12:09

1 Answers1

0

You have to control the output of your python script depending on the success of the script. For example via exit(1). I would probably start the python scripts via powershell. You are able to get the exit code of applications via $LASTEXITCODE.

python code (script does not run as intended):

exit(1)

PS code:

#run python script here
if ($lastexitcode -gt 0){
    #send mail
}
hcm
  • 922
  • 3
  • 10
  • Exit will only exit to exit the script, how will it test if the script has run successfully ? – Mar Dec 16 '19 at 12:46
  • @MarieAntoinette edited my answer. You will have to determine yourself if the python script failed or not. – hcm Dec 16 '19 at 12:55
  • Actually this is my main question. How can I know that my python script failed or not so that i can condition the sending emails. – Mar Dec 16 '19 at 12:57
  • @MarieAntoinette Please reword your question since this is not clear. You then should also post your code, else it's hard to help. – hcm Dec 16 '19 at 12:59
  • I have a python script that i run via a cmd script, at the end i send an email containing a log file and i want to condition my sending emails. if the python script is not okay then send the email. – Mar Dec 16 '19 at 13:03
  • @MarieAntoinette I meant edit your question, your first post in this thread. Add the code of the scripts. – hcm Dec 16 '19 at 13:09