-1

I have a simple script file startsql.sh to start mysql:

#!/bin/bash
#Script to Start MySQL
echo "Starting MySQL"
if sudo service mysqld start; then
    echo "MySQL started successfully!"
else
    echo "Error: Failure to start MySQL" 1>&2
    exit 1
fi

I run it using:

bash startsql.sh |& tee -a scriptlogs.log

Though the service seems to start successfully, the command hangs after showing the messages on the command window. It works fine without hanging if I remove the tee. Interestingly I have a similar script to stop mysql and it works fine without issues. I checked and find no difference between the two scripts.

After searching a lot, I found that using the below works, but the side effect of this is that the tee process is still running in the background

bash startsql.sh > >( tee -a scriptlogs.log) 2>&1

Can someone please help me understand why does using tee hang on some occasions.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

0

Will you teach me the version of bash? If you use bash 3.2.57, the error occure like this.

$ bash ./startsql.sh |& tee -a output.txt
bash ./startsql.sh |& tee -a output.txt
bash: syntax error near unexpected token `&'
Yuji
  • 525
  • 2
  • 8
  • The bash version is 4.1.2(1)-release (x86_64-redhat-linux-gnu) – Dipen Parekh Sep 16 '19 at 04:07
  • Please note that if you are trying to reproduce the issue, you will have to stop mysql first and then run this script. It works fine if the mysql is already running. Thanks – Dipen Parekh Sep 16 '19 at 04:16