send email when script stops at error or finishes

Asked by BOCHENG YIN

I am pretty new in sikuli coding.
I want to have sikuli 1.1.4 to send a message to my email when the script stops at errors or finishes.
That would help me to monitor an automated unattended work coordinated by sikulix.
I find a relative post years ago, around 2015, but don't know how to make it work in sikuli 1.1.4.
Thank you so much if you have any solution.
Best, bocheng

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
BOCHENG YIN
Solved:
Last query:
Last reply:
Revision history for this message
Mike (maestro+++) said :
#1

Remember Sikulix is a framework for scripting visual interfaces rather than APIs. So rather than trying to use something like smtplib (which doesn't seem to work anyway) why not choose a GUI mail interface eg Outlook? Keep Outlook/GMail/whatever open and get that to send emails for you..

Revision history for this message
BOCHENG YIN (borishappy2009) said :
#2

Mike Thanks! This makes sense to use Outlook or Gmail.

However, I want to send a message to warn my work is finished or there is a problem that Sikuli crashes.
I am expecting to see any case that a Sikuli script fails, not limited to a specific error that is linked to a certain line of code.

In that way, the message sending command is not in the specific script I run, but in the Sikuli setup.
or
 I have two Sikuli scripts will run in a queue? The first one is doing the jobs I want to monitor. The second is initiated by the finish or the fail of the first script and sending a message by calling an email GUI

Is it possible to do that?
Thanks!

Revision history for this message
Mike (maestro+++) said :
#3

I've not done this personally.

You can read about RobotFramework here:

sikulix-2014.readthedocs.io/en/latest/scenarios.html

Command line switches which might help you capture error messages are described here:

https://sikulix-2014.readthedocs.io/en/latest/faq/010-command-line.html

Revision history for this message
RaiMan (raimund-hocke) said :
#4
Revision history for this message
BOCHENG YIN (borishappy2009) said :
#5

I think I get the idea to nest a script inside another script. if the nested script fails, it is possible not to halt the host script, but continue.
I create a test code. Please see below:
try:
    runScript("./test2.sikuli")
except:
    print("sikuli crashes")

I can run the code if all of the targets in test2.sikuli are visible.

Then I block one of the targets to deliberately disrupt the try part of the script. The script is terminated immediately.
I try to add an exit() parameter to the runScript() code line. now it is like this: runScript("./test2.sikuli",exit(1)). Now it just run the except clause.

I think I just don't know how to make it work in an expected way. Could anyone help?
Thanks a lot!

Revision history for this message
RaiMan (raimund-hocke) said :
#6

--- runScript("./test2.sikuli",exit(1)).
not valid and hence excepts ;-)

try:
    runScript("./test2.sikuli")
    exit(0) # processed if run ok
except:
    print("sikuli crashes")
    exit(1) # processed if crashed

Revision history for this message
BOCHENG YIN (borishappy2009) said :
#7

Hi, RailMan,

I try your code as below:
____________________
try:
    runScript("./testClick.sikuli")
    exit(0) # processed if run ok

except:
    print("sikuli crashes")
    exit(1)
___________________
testClick.sikuli has code as below:
click(find("1552251843198.png"))
print("find it")
_______________________
when the target image is visible, the result message is
"
find it
sikuli crashes
[info] Exit code: 1
"
When the target images is blocked, the result message is
"
....FindFailed...
sikuli crashes"
[info] Exit code: 1
 ".

Why it always run the except clause no matter that the script in "try" section can finish or not.
Thanks a lot!
-Bocheng

Revision history for this message
RaiMan (raimund-hocke) said :
#8

No idea, why, but exit() in the try block leads to the processing of the except block.

so do it this way:

try:
    runScript("./testClick.sikuli")

except:
    print("sikuli crashes")
    exit(1)

exit(0) # only processed if run ok

Revision history for this message
BOCHENG YIN (borishappy2009) said :
#9

Hi, RailMan,
I just try it. It doesn't work.
Let me summarize:
[1]if like this:
++++++++++++++++++++++
try:
    runScript("./click.sikuli")
    exit(0)
except:
    print("sikuli crashes")
    exit(1)
++++++++++++++++++++++
then it always run the except clauses no matter the testClick script works or not.

[2]if like this:
++++++++++++++++++++++
try:
    runScript("./click.sikuli")

except:
    print("sikuli crashes")
    exit(1)
exit(0)
+++++++++++++++++++++
then it always report "Exit code 0", never run the except clauses no matter the testClick script works or not.

[3] if like this
++++++++++++++++++++++
try:
    runScript("./click.sikuli")

except:
    print("sikuli crashes")
    exit(1)
+++++++++++++++++++++++
then it never report Exit code, never run the except clauses no matter the testClick script works or not.

Thanks!

Revision history for this message
RaiMan (raimund-hocke) said :
#10

I am sorry, for misleading you.
I now finally tried myself and had a look into the implementation.

This is what currently works in your sense:

exitValue = runScript(whatever)
if exitValue == 1:
    print "there was an exception"
    exit(1)
else:
    print "ran with success"
    exit(exitValue)

Your whatever-script should not terminate with exit(1), since this is "reserved" for signaling an exception during runScript (which should be shown as [error] messages)

In the outside world (command line) you can deal now with the returned value.

Revision history for this message
RaiMan (raimund-hocke) said :
#11

I added a note to the docs

Revision history for this message
BOCHENG YIN (borishappy2009) said :
#12

It works! Thank you so much! RaiMan.