Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
10-25-2016, 01:07 PM
|
#1
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Rep: 
|
#!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:
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.
|
|
|
10-25-2016, 01:18 PM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,751
|
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.
|
|
|
10-25-2016, 04:57 PM
|
#3
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Original Poster
Rep: 
|
Will do tomorrow, when I'm back at work and have access to the code again.
|
|
|
10-25-2016, 07:49 PM
|
#4
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
I think 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.
|
|
|
10-26-2016, 01:22 AM
|
#5
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
> /dev/null 2>&1
afaik this should suppress ALL output, even if the command is faulty?
|
|
|
10-26-2016, 02:21 AM
|
#6
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Original Poster
Rep: 
|
Here's the full script:
Code:
#!/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
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.
|
|
|
10-26-2016, 02:35 AM
|
#7
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,751
|
Thanks.
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)
|
|
|
10-26-2016, 03:03 AM
|
#8
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Original Poster
Rep: 
|
Quote:
Originally Posted by Turbocapitalist
Thanks.
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.
|
|
|
10-26-2016, 03:15 AM
|
#9
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Original Poster
Rep: 
|
Just tried with set -e -x and it is indeed the last line that's outputting.
What I did see though was that the command shown was:
Code:
mysql -u<user> -p<pass> '-AeKILL QUERY <id>;'
Which seems different from what's in my code, not only missing the > /dev/null 2>&1 part, but also having single quotes and missing double quotes:
Code:
mysql -u${mysqlUser} -p${mysqlPass} -Ae"KILL QUERY ${SLEEP_ID};" > /dev/null 2>&1
|
|
|
10-26-2016, 04:58 AM
|
#10
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797
|
It's the sleep query running in background that produces this output, see the docs here:
http://dev.mysql.com/doc/refman/5.7/...function_sleep
Code:
This statement is interrupted using KILL QUERY from another session:
mysql> SELECT SLEEP(1000);
+-------------+
| SLEEP(1000) |
+-------------+
| 1 |
+-------------+
|
|
|
10-26-2016, 05:19 AM
|
#11
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Original Poster
Rep: 
|
@keefaz
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?
Thank you for your answer though.
|
|
|
10-26-2016, 05:30 AM
|
#12
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,751
|
Quote:
Originally Posted by RagingRaven
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.
|
|
|
10-26-2016, 05:35 AM
|
#13
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Original Poster
Rep: 
|
I'll give it a go with > /dev/null 2>&1 at the end of that line then.
Edit: no change with this edit, I still get output.
Last edited by RagingRaven; 10-26-2016 at 06:39 AM.
|
|
|
10-26-2016, 06:05 AM
|
#14
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797
|
You can put the command in a subshell like
Code:
( command -args "bla" & )
|
|
|
10-26-2016, 06:47 AM
|
#15
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Original Poster
Rep: 
|
What I'm noticing after the last edit is that again the > /dev/null 2>&1 part looks like it gets removed from the command:
The 'set -e -x' output shows the command as:
Code:
mysql -u<user> -p<pass> '-AeFLUSH TABLES WITH READ LOCK; SELECT SLEEP(86400)'
Where in the code it is:
Code:
mysql -u${mysqlUser} -p${mysqlPass} -Ae"FLUSH TABLES WITH READ LOCK; SELECT SLEEP(86400)" & > /dev/null 2>&1
So it looks to me like the double quotes might have something to do with it, everything behind it appears to get removed.
|
|
|
All times are GMT -5. The time now is 08:59 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|