You say you want stderr and stdout to appear in the console, but only stderr (not stdout), which contains "ERROR" to enter the file? This is the last condition that makes it difficult to find an elegant solution. If this is what you are looking for, here is my very ugly solution:
touch stm.out stm.err stm 1>stm.out 2>stm.err & tail -f stm.out & tail -f stm.err & \ wait `pgrep stm`; pkill tail; grep ERROR stm.err > error.log; rm stm.err stm.out
I warned you that this is ugly. You can hide it in a function, use mktemp to create temporary file names, etc. If you do not want to wait for stm to exit before writing ERROR text to a file, you can add tail -f stm.err | grep ERROR > error.log & tail -f stm.err | grep ERROR > error.log & after the other tail commands, and remove the grep command from the last line.
Markku K.
source share