LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-10-2008, 01:20 PM   #1
pgb205
Member
 
Registered: Nov 2007
Posts: 109

Rep: Reputation: 15
writing output of a program to a file only when all of it is ready


I run a mysql batch job and want it's results redirected into a text file.
mysql -t -h localhost -u user -p=password <queryfile> output
I pull up this file(output) in my browser to view results.

the only problem with this seems to be that as soon as mysql query starts executing it opens up the output file and deletes its contents so there are few seconds when this file is empty. Is there any way to redirect output to a file so that it doesn't write or open a file untill all of it's data is ready. Then it should open file for writing and delete it's results.

Hope this made sense and thanks for all your replies.
 
Old 12-10-2008, 01:23 PM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,022

Rep: Reputation: 173Reputation: 173
As far as I'm aware the answer is No.
What you could do though is write the output of the mysql command to a different file, then cat the contents of that to the file output.
E.g.

Code:
foo=`mktemp`
mysql -t -h localhost -u user -p=password <queryfile> $foo
cat $foo > output
rm $foo
 
Old 12-10-2008, 01:36 PM   #3
pgb205
Member
 
Registered: Nov 2007
Posts: 109

Original Poster
Rep: Reputation: 15
amazing. exactly what i needed. thanks a bunch
 
Old 12-10-2008, 01:41 PM   #4
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,022

Rep: Reputation: 173Reputation: 173
It just occured to me that if you were to do

Code:
(mysql -t -h localhost -u user -p=password <queryfile) < output
then that might work. Maybe. Or not. Can't be bothered to create a test to be honest
 
Old 12-10-2008, 02:01 PM   #5
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
Originally Posted by arizonagroovejet View Post
As far as I'm aware the answer is No.
What you could do though is write the output of the mysql command to a different file, then cat the contents of that to the file output.
E.g.

Code:
foo=`mktemp`
mysql -t -h localhost -u user -p=password <queryfile> $foo
cat $foo > output
rm $foo
I'm not sure whether using cat would take an exclusive lock on its output file, so it may still be possible to get an incomplete file if accessed while cat was still running.
( edit: just tried it by doing a
yes | cat - >/tmp/yes.out &
cat /tmp/yes.out
and the redirection does not lock the output file)
)

'mv' might be a better choice than cat as it's a more atomic operation and as long as it's moving within the same filesystem wouldn't require the extra copying of data.

Code:
foo=`mktemp`
mysql -t -h localhost -u user -p=password <queryfile> $foo \
&& mv $foo output
Also note the use of && to only move on success of the mysql command.

disclaimer: I've not played with mysql, so I don't know whether it returns 0 on success or not, but it gives you something more to think on.

Last edited by GazL; 12-10-2008 at 02:07 PM.
 
  


Reply

Tags
mysql, redirect


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
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
in Pascal: how to exec a program, discard text output or send to text file Valkyrie_of_valhalla Programming 6 05-02-2007 09:50 AM
writing a simple file shredding program qwerty Linux - General 6 01-31-2006 01:21 PM
writing output to file Hockeyfan Linux - General 3 10-24-2005 08:02 PM
writing a c program in linux which plays an'.wav' file stephenwalter Programming 8 07-08-2005 11:37 PM


All times are GMT -5. The time now is 05:29 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration