LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   writing output of a program to a file only when all of it is ready (http://www.linuxquestions.org/questions/linux-software-2/writing-output-of-a-program-to-a-file-only-when-all-of-it-is-ready-689642/)

pgb205 12-10-2008 02:20 PM

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.

arizonagroovejet 12-10-2008 02:23 PM

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


pgb205 12-10-2008 02:36 PM

amazing. exactly what i needed. thanks a bunch

arizonagroovejet 12-10-2008 02:41 PM

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 :)

GazL 12-10-2008 03:01 PM

Quote:

Originally Posted by arizonagroovejet (Post 3370737)
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.


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