I have a script (eg. compute.sh) that does something on my Ubuntu 16.04 and may end with error, let's say:
[compute.sh]
#!/bin/bash
...
MISCELLANEOUS OPERATIONS
...
[if error occours]
echo "Error 55a" >&2;
exit 1;
...
echo "Execution ok";
exit 0;
and another script that handles errors (eg. error_handler.sh)
[error_handler.sh]
error=$(cat)
for i in "$@"
do
case $i in
-u=*|--user=*)
username="${i#*=}"
shift
;;
-t=*|--task=*)
task="${i#*=}"
shift
;;
esac
done
...
SENDS ERROR REPORTING THROUGH AN API LISTENING REMOTELY
...
I'd like to execute compute.sh, redirecting its stderr to error_handler.sh in order to notify my API system the error occourred
if I try this test:
user@ubuntu: echo "test message" | ./error_handler.sh --user=dude --task="simple task"
my error_handler.sh script takes the string "test message" and correctly handles it
how can I redirect only the stderr output to my error_handler.sh script?
EDIT: as sometimes compute.sh may fail without an error message (just by doing exit 1;) I'm not sure if error=$(cat) it's the right way to catch the error message in error_handler.sh. Any other option?
EDIT2: the task may be executed in a crontab, so I need to do all in the same command