Search code examples
bashshellstderr

Redirecting STDERR in a Bash file results in a file created even when there is no error. Why?


I've created a small Bash script that does a MySQL data dump. Because the dump can be fairly large I put the process in the background and then wait for either an error or the log to show up in the file system. I have the following code:

mysqldump main_db > /loc/to/dmp/file.sql 2>/loc/to/error/log/file.log &

The problem is that I get a '/loc/to/error/log/file.log' file the size of 0 (which I presume means no real error) sometimes when this command is run, which kills the process, even though there is no error.

I'm not sure why the STDERR would write a file when there was no data to write. Is this because of the & background process?


Solution

  • The redirection file is created whether or not any data is ever written to it. Whichever process is watching the error log should check for non-zero filesize, not existence.