Astora
Astora

Reputation: 725

MySQL: How to redirect source command output?

If I execute

tee /tmp/script.log
source /root/myscript.sql

I get the log in script.log and in my current prompt session.

I want to execute myscript.sql and don't show anything in my prompt, only save it in script.log

I've tried mysql -s (--silent), but then no log is generated in script.log.

I need to use source command, mysql < myscript.sql is not userfull because it leave the session when its finish, and I have flush tables with read lock command in the end of the script, if the session leaves, flush tables with read lock is terminated.

Upvotes: 0

Views: 47

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562911

( cat myscript.sql && read ) | mysql

Because read is pausing waiting for input, the input to mysql doesn't finish, and mysql doesn't exit. So the lock held by FLUSH TABLES WITH READ LOCK stays active as long as you want.

When you want to terminate the read, just hit return. Or type anything, as long as it doesn't cause an error as input to mysql.

If you want it to terminate itself after say 60 seconds, you could alternatively do this:

( cat myscript.sql && sleep 60 ) | mysql

Upvotes: 1

Related Questions