Page 1 of 1
Send e-mail notification after script success/failure
Posted: Sun Feb 02, 2014 12:34 pm
by breq
Hi,
I have a task which runs php script.
It would be nice to have an email notification after failure/success of task and if task was executed successfully BUTT php script returns nothing or it crashes (ex. script get data from database, if there is no data for today) it should e-mail me that there was some error.
Is it possible to do?
Re: Send e-mail notification after script success/failure
Posted: Mon Feb 03, 2014 10:39 am
by Oleg
How do you run these scripts: by command line or by using some web URL?
Re: Send e-mail notification after script success/failure
Posted: Mon Feb 03, 2014 12:03 pm
by breq
Like this:

- Bez tytułu.png (12.93 KiB) Viewed 35152 times
Re: Send e-mail notification after script success/failure
Posted: Mon Feb 03, 2014 1:42 pm
by Oleg
There are two ways to achieve this.
1-st way.
Put into PHP script such code
die(<exit_code>);
-or-
exit(<exit_code>);
I use for my test simple script:
i.e. my script generates the exit code 10.
You can catch and analyze this code in RoboTask (see screenshot)

- CatchExitcode.png (42.02 KiB) Viewed 35151 times
See full task below:
Code: Select all
;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|317955339
Comment=STRINGLIST
ContinueOnError=INTEGER|0
ExternalName=STRING|"Task841"
Hide=INTEGER|0
ID=INTEGER|436396385
LogOnAsUser=INTEGER|1
Name=STRING|"Run php and return code"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
ToLog=INTEGER|3
WriteGeneralLog=INTEGER|0
[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER
[Actions\Action1]
ActionID=STRING|"A_VARIABLES_CREATE"
Enabled=INTEGER|-1
Name=STRING|"Create local variable ""ERR"" with value ""0"""
Params=FOLDER
[Actions\Action1\Params]
expand=STRING|"0"
varkind=STRING|"1"
varname=STRING|"ERR"
varvalue=STRING|"0"
[Actions\Action2]
ActionID=STRING|"A_GENERAL_RUN_PROG"
Enabled=INTEGER|-1
Name=STRING|"Run ""php.exe ""D:\test\php\script1.php"" """
Params=FOLDER
[Actions\Action2\Params]
ifnonzero=STRING|"0"
params=STRING|"""D:\test\php\script1.php"" "
program=STRING|"C:\Program Files (x86)\PHP\v5.3\php.exe"
runas=STRING|"0"
variable=STRING|"ERR"
wait=STRING|"2"
[Actions\Action3]
ActionID=STRING|"A_FLOW_IF"
Enabled=INTEGER|-1
Name=STRING|"If Then"
Params=FOLDER
[Actions\Action3\Params]
case=STRING|"0"
operator=STRING|"1"
type=STRING|"1"
value1=STRING|"{err}"
value2=STRING|"0"
[Actions\Action4]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""Error: {err}"""
Params=FOLDER
[Actions\Action4\Params]
icon=STRING|"3"
msg0=STRING|"Error: {err}"
msgcount=STRING|"1"
playsound=STRING|"0"
showmessage=STRING|"1"
[Actions\Action5]
ActionID=STRING|"A_FLOW_ENDIF"
Enabled=INTEGER|-1
Name=STRING|"End If"
2-nd way
You can save the output result of the script into a file and analyze this text.
You can test this text on specific word or phrase (for example the word "Error")
I use such script for this example:
Code: Select all
<?php
die('Error: some problem');
?>
See the task below:
Code: Select all
;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|317955339
Comment=STRINGLIST
ContinueOnError=INTEGER|0
ExternalName=STRING|"Task843"
Hide=INTEGER|0
ID=INTEGER|779559767
LogOnAsUser=INTEGER|1
Name=STRING|"Run php and return text"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
ToLog=INTEGER|3
WriteGeneralLog=INTEGER|0
[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
[Actions\Action1]
ActionID=STRING|"A_GENERAL_RUN_PROG"
Enabled=INTEGER|-1
Name=STRING|"Run ""cmd.exe /c """"C:\Program Files (x86)\PHP\v5.3\php.exe"" ""D:\test\php\script2.php"""" > {TempDir}\out.txt"""
Params=FOLDER
[Actions\Action1\Params]
ifnonzero=STRING|"0"
params=STRING|"/c{EOL}""""C:\Program Files (x86)\PHP\v5.3\php.exe""{EOL}""D:\test\php\script2.php"""" > {TempDir}\out.txt"
program=STRING|"cmd.exe"
runas=STRING|"0"
variable=STRING|"ERR"
wait=STRING|"2"
[Actions\Action2]
ActionID=STRING|"A_FLOW_IF"
Enabled=INTEGER|-1
Name=STRING|"If Then"
Params=FOLDER
[Actions\Action2\Params]
case=STRING|"0"
operator=STRING|"6"
type=STRING|"0"
value1=STRING|"error"
value2=STRING|"{TextFile({TempDir}\out.txt)}"
[Actions\Action3]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""Error: """
Params=FOLDER
[Actions\Action3\Params]
icon=STRING|"3"
msg0=STRING|"Error: "
msg1=STRING|"{TextFile({TempDir}\out.txt)}"
msgcount=STRING|"2"
playsound=STRING|"0"
showmessage=STRING|"1"
[Actions\Action4]
ActionID=STRING|"A_FLOW_ENDIF"
Enabled=INTEGER|-1
Name=STRING|"End If"
Save the text of the task ino a file and use menu
Task->Import in order to import into RoboTask.
Re: Send e-mail notification after script success/failure
Posted: Mon Feb 03, 2014 8:12 pm
by breq
Ok, last question. Why if I die/exit script like this
it shows me alert
Re: Send e-mail notification after script success/failure
Posted: Mon Feb 03, 2014 8:56 pm
by Oleg
Maybe it depends on PHP version
I use PHP 5.3.
Try to use expression
exit(99);
My PHP processes die and exit expressions equally
Re: Send e-mail notification after script success/failure
Posted: Mon Feb 03, 2014 9:49 pm
by breq
exit also return 1 as variable, not 99. That's weird...