[SOLVED] #!bin/sh script mysql command won't redirect to /dev/null
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!bin/sh script mysql command won't redirect to /dev/null
I've created a script with bin/sh to do some work on my mysql database.
Before doing work on the database I do a "FLUSH TABLES WITH READ LOCK; SELECT SLEEP (86400)" QUERY which is all working fine
But later on in the script i'm trying to do a "KILL QUERY <id>" command and I don't want to see the output, but for some reason I can't get it to redirect to /dev/null
The exact command is:
Code:
mysql -u<user> -p<password> -Ae"KILL QUERY <id>;" > /dev/null 2>&1
Where <user> is the database user, <password> is the database password and <id> is the correct query id
The command itself works fine as the query gets killed as expected, but it keeps outputting:
Code:
SLEEP(86400)
1
I've tried various ways of calling the command, like wrapping it in `` and $(), but I keep getting the output.
It's probably something simple I'm overlooking, but I can't seem to figure it out.
Can you post the exact script here, in [code] tags, minus the sensitive parts? It might be the greater than and less than signs < > in the query messing things up.
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
Rep:
I think
Code:
2>&1 &
should work, and maybe a space between -Ae and the opening quote. And you can try placing the semi after the last "&". But I'm not completely positive on that last thing.
#!/bin/sh
#Get root's path and variables
. /root/.bashrc
errors=0
mysqlUser="<mysqluser>"
mysqlPass="<mysqlpass>"
#lock tables
mysql -u${mysqlUser} -p${mysqlPass} -Ae"FLUSH TABLES WITH READ LOCK; SELECT SLEEP(86400)" &
`sleep 5`
#get processlist
mysql -u${mysqlUser} -p${mysqlPass} -ANe"SHOW PROCESSLIST" | grep "SELECT SLEEP(86400)" > proclist.txt
#get id
SLEEP_ID=`cat proclist.txt | awk '{print $1}'`
#do work on database
#haven't gotten around to this yet.
#remove lock on tables
mysql -u${mysqlUser} -p${mysqlPass} -Ae"KILL QUERY ${SLEEP_ID};" > /dev/null 2>&1
The last line is the line that outputs
Code:
SLEEP(86400)
1
Keep in mind that the command itself seems to work fine, the query gets killed as expected, but it's just the '> /dev/null 2>&1' part that's not working.
If you make the second line of the script set -e -x it can help with some debugging as it will show the exact line, with variables filled in, before it is run. That will show you which line is actually producing the output in question. My guess is that it might be the table-locking line.
If you make the second line of the script set -e -x it can help with some debugging as it will show the exact line, with variables filled in, before it is run. That will show you which line is actually producing the output in question. My guess is that it might be the table-locking line.
Also, SLEEP_ID can be assigned without "cat"
Code:
SLEEP_ID=$(awk '{print $1}' proclist.txt)
I'll edit my script as you said, but I'm pretty sure it's the last line, as when I put echo's before each command the output mentioned comes right after the last echo.
This statement is interrupted using KILL QUERY from another session:
mysql> SELECT SLEEP(1000);
+-------------+
| SLEEP(1000) |
+-------------+
| 1 |
+-------------+
Ok that sounds good, but the question is, how would I get rid of the output?
Do I have to do something to the
Code:
mysql -u${mysqlUser} -p${mysqlPass} -Ae"FLUSH TABLES WITH READ LOCK; SELECT SLEEP(86400)" &
line?
I thought the &-sign at the end ment the command would run in the background and therefor not produce output?
Would adding a > /dev/null 2>&1 at the end of that command work?
I thought the &-sign at the end ment the command would run in the background
The ampersand makes it run in the background but it will still produce output, even while other programs are running. You can try with some other program besides the database client to experiment.
You'd need to redirect the output of that line (I referred to it as the table-locking line) if you don't want output.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.