LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 10-25-2016, 01:07 PM   #1
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Rep: Reputation: Disabled
#!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.
 
Old 10-25-2016, 01:18 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,160
Blog Entries: 3

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
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.
 
Old 10-25-2016, 04:57 PM   #3
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Original Poster
Rep: Reputation: Disabled
Will do tomorrow, when I'm back at work and have access to the code again.
 
Old 10-25-2016, 07:49 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004
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.
 
Old 10-26-2016, 01:22 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,456
Blog Entries: 9

Rep: Reputation: 3370Reputation: 3370Reputation: 3370Reputation: 3370Reputation: 3370Reputation: 3370Reputation: 3370Reputation: 3370Reputation: 3370Reputation: 3370Reputation: 3370
> /dev/null 2>&1

afaik this should suppress ALL output, even if the command is faulty?
 
Old 10-26-2016, 02:21 AM   #6
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Original Poster
Rep: Reputation: Disabled
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
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.
 
Old 10-26-2016, 02:35 AM   #7
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,160
Blog Entries: 3

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
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)
 
Old 10-26-2016, 03:03 AM   #8
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
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.
 
Old 10-26-2016, 03:15 AM   #9
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-26-2016, 04:58 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
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 |
+-------------+
 
Old 10-26-2016, 05:19 AM   #11
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Original Poster
Rep: Reputation: Disabled
@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.
 
Old 10-26-2016, 05:30 AM   #12
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,160
Blog Entries: 3

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Quote:
Originally Posted by RagingRaven View Post
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.
 
Old 10-26-2016, 05:35 AM   #13
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-26-2016, 06:05 AM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
You can put the command in a subshell like
Code:
( command -args "bla" & )
 
Old 10-26-2016, 06:47 AM   #15
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply

Tags
mysql, output, redirect, stderr, stdout


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can i redirect any command to dev null? postcd Linux - General 3 04-18-2014 10:03 AM
[SOLVED] Perl: redirect STDERR from system() call to /dev/null totaluser Linux - Newbie 2 11-25-2011 02:30 AM
/dev/null file mod changes won't stick on reboot wonderboy1999 MEPIS 10 09-04-2007 11:07 AM
Redirect X windows / GUI to /dev/null? mattfr Linux - Software 9 12-20-2004 09:40 AM
Outputting Command Text to /dev/null rootking Linux - Software 4 12-19-2004 07:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration