Search in logfile
I have some legacy softwares that I need to automate under Control-M. These job are under Windows 2008R2.
These jobs have an exit code 0 if they run ok, but also if they can manage some errors. I need to raise an alarm when a specific string is in the log.
The string is not in the output of executable.
I implemented another job for this. It goes to search a string inthe file and in "On Do Actions" I search for the statement.
To have the statement in the output I think to use something like a grep . I used:
findstr
findstr "myerrorcode" D:Loggreptest_%%$ODATE..log
grep
under cygwin
In both case I have an identical situation:
But the problem is: when the string is not found in the file, both grep and findstr have a return code = 1.
How can I discriminate the cases when the file cannot be open and when everything runs ok, but the sring in the log is not found?
You should be able to use grep
's exit status to detect the reason for failure. According to the POSIX grep
docs, exit status section:
EXIT STATUS
The following exit values shall be returned:
0 One or more lines were selected.
1 No lines were selected.
>1 An error occurred.
It's similar for GNU grep
(consistent, but more specific):
Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. [...] Other grep implementations may exit with status greater than 2 on error.
For example, in bash
, you could use the case
command to handle multiple branches like this:
#!/bin/bash
# search for error code in file
grep code file
# store the exit status in variable err
err=$?
# test several cases
case $err in
0) echo All good.;;
1) echo Code not found.;;
*) echo Error reading from file.;;
esac
You can handle this within Control-M easily: Add in Job Tab "Action"
A character string, from 1 through 132 characters in length, containing a statement from the job script file The specified string can be a portion of the statement.
Statement character strings can each contain mask characters. Valid mask characters are:
* – represents any number of characters (including no characters)
$ – represents any single character
? – represents any single character
A character string, from 1 through 255 characters in length, to be compared to the operating system's response to the specified statement.
Code character strings can each contain mask characters. Valid mask characters are:
* – represents any number of characters (including no characters)
$ – represents any single character
? – represents any single character
Example: On Do Action Control-M 8
链接地址: http://www.djcxy.com/p/41220.html下一篇: 在日志文件中搜索