15

My log files are getting dumped with following message while running shell scripts using some underlying MySQL commands.

Here is the message:

"Warning: Using a password on the command line interface can be insecure."

To stop these messages, I am using the following job definition.

Example:

run_wrapper.sh |grep -v "Warning: Using a password" > output.log 2>&1

This worked but the MySQL errors are not being logged to output.log.

If I change the definition like the following, then MySQL errors start appearing if any

run_wrapper.sh > output.log 2>&1

So the question is how to suppress the warning messages and also report SQL errors in log files using only the cron definition?

3
  • probably you should use run_wrapper.sh >> output.log 2>&1
    – Rahul
    Jul 5, 2016 at 9:17
  • 1
    I'm not following. Where would you want error/warnings to go and where would you want output to go, and what would you want to throw away? What is generating the password warning and why do you have errors in the MySQL that you don't want to fix?
    – Kusalananda
    Jul 5, 2016 at 9:38
  • 1
    mysql prints that warning when you use the -p option on the command line. Instead of fixing it (e.g. by creating a ~/.my.cnf with 600 perms) the OP wants to just ignore and discard the warning (and only that warning, not all of stderr)
    – cas
    Jul 5, 2016 at 13:10

5 Answers 5

31

In your bash script, edit it at top

export MYSQL_PWD=yourdbpassword

and mysql query like: mysql -u username -h host db -e "statement"

Reference: From answer posted at Stackoverflow. Other answers can also be followed.

4

search for a line in your wrapper similar to

mysql -u<username> -p<some_password> -hlocalhost -D<database_name> 

and change to

export MYSQL_PWD=<some_password> ; mysql -u<username> -hlocalhost -D<database_name> 

this solves the source of the warning.

1
  • This makes sense :-) Thx!
    – Mike Q
    Dec 30, 2019 at 17:56
1

It looks like you're missing the stderr redirection from run_wrapper.sh itself, so the errors aren't going through the grep and thence to the log file.

Try this instead if you're happy to have both stdout and sdterr written to your logfile

run_wrapper.sh 2>&1 | grep -v "Warning: Using a password" > output.log

Or if you want only the errors written to the logfile, and stdout left writing to the calling terminal, try this

( run_wrapper.sh 2>&1 1>&3 | grep -v "Warning: Using a password" > output.log ) 3>&1
0

Try this:

lf='output.log'
> "$lf"   # first truncate/create the logfile.
run_wrapper.sh >> "$lf" 2> >(grep -v "Warn.*passw.*insec" >> "$lf")

Redirects stderr via Process Substitution to grep -v ..., and output from that is appended with >> to output.log

You probably want to use (GNU) grep's --line-buffered option as well as -v to make sure error output isn't delayed.


If post-processing the log file is an acceptable option for you, you could just delete the unwanted "Warning: " line(s) from the log file after run_wrapper.sh has finished.

The following shell script fragment saves the timestamp (in $ts) of the log file (in $lf) before running sed -i and restores it afterwards:

lf='output.log'

run_wrapper.sh >& "$lf"

ts=$(date -r "$lf" '+%Y%m%d%H%M.%S')
sed -i -e '/Warning: Using a password/d' "$lf"
touch -t "$ts" "$lf"

If you need to preserve the inode of the log file (e.g. because it has hard links), use ed rather than sed:

ts=$(date -r "$lf" '+%Y%m%d%H%M.%S')
printf "%s\n" 'g/Warning: Using a password/d' w | ed -s "$lf"
touch -t "$ts" "$lf"
4
  • Thanks for the response. I have tried the suggested option like below Jul 6, 2016 at 5:13
  • sed option creates blank lines and we need to remove them also and it changes file timestamp also Jul 8, 2016 at 5:06
  • The post-process-with-sed option also changes the inode as well as the timestamp - that's normal for -i (and most forms of "in-place" editing"). If run immediately afterwards, it should only be microseconds or seconds different, depending on logfile size of course. But there's no way a d (delete line) command in sed will create a blank line - what exactly do you mean by that?
    – cas
    Jul 8, 2016 at 5:15
  • @jagadishpuvvada sed option now saves and restores output.log's timestamp.
    – cas
    Jul 8, 2016 at 12:06
0

Try adding this to the end of your command:

/dev/null 2>&1

You must log in to answer this question.

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