3

I am trying to get the log from MySQL into a file when executing a script from the command line.

For example

mysql -v mydb u- alex -p**** < please_run_me.sql > /tmp/file.log

Now, the work gets done, but file.log is empty. I thought that -v would give me the desired output. I would like to understand how I

  1. Get output from MySQL into file.log
  2. Or better, only get content into file.log if the job fails (but both is handy as when I am testing far the command line I would like to know what is going on)
  3. Emails myself the error and/or whatever

(Eventually this will end as a cron job).

1 Answer 1

3

You need to redirect stderr to get the log output when using -v.

You need to do something like:

mysql -v mydb u- alex -p**** < please_run_me.sql 2>&1 > /tmp/file.log

The 2>&1 redirects stderr to stdout, which you then redirect to /tmp/file.log. Remember that > will overwrite /tmp/file.log - use >> if you'd like to append to it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .