I've a script which do. ls , cp, mv I want if any of the command fail this should alert over email as well as after successful competition. Any idea? I can adjust email alert but want to know how to check for errors and successful attempt.
|
You can have a series of commands continue to execute until "failure" by using "&&" to run the commands in succession; each command returns "true", causing the following command to run. Then use "||" to run a command upon failure, such as:
Since that can get messy, use functions, such as
Further, continuing from the above example, you can add blanket rules via a 'trap' in the script that will always execute certain commands if any type of error status is returned at any point in the script (including receiving a control-c (interrupt) signal while the script is running):
(Disclaimer... commands shown above are general pseudo-code, and just typed from memory without running them. Typos surely exist.) |
|||
|
|
|
Bash doesn't have any built in alerting for this stuff, you need to write it into your script by checking the A dead simple check that emails would look like:
Be sure to check your command documentation to make sure successful execution sets an exit code of 0. |
|||
|
|
|
You use a mailwrap command wrapper, one which takes some options specifying who to mail to, before running the command which is its parameter. The mailwrap command only sends email if there's output, or if the script failed. There are many variants out there, and they're good exercises to write yourself if you don't like those you find. On the To have a failing command not abort the script, you append If you want to see exactly where a normally-silent command failed, you use a mailwrap command which doesn't send email just because of output to stderr and then you use |
|||
|
|
|
Or pipe the output of the script into a mail whatever the result is until you're happy it's working. Something like:
|
||||
|
|
lswould befind -maxdepth 1 -ls, mv/cp could be replaced by a python/perl script or a combination withtar. Of course, it's possible to place your own binaries on the system and execute them. What would you like to accomplish? – Lekensteyn Mar 27 '11 at 19:50