LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-11-2010, 09:33 PM   #1
sanimfj
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Rep: Reputation: 0
netcat / bash predicament


I have a quick question about netcat.


My job requires me to regularly perform backups from some of our remote database servers. I have been utilising bash to achieve this:

mysqldump tablename | gzip | netcat (myip) (myport)

and on the local end

netcat -l -p (myport) > backup.sql.gz


This works great and I quite like the setup. the only problem is that the connection does not close when the command is complete. So I am currenly regularly checking the file system to see if the file size of the backup is still increasing (ie it is still downloading.) and then ctrl-c ing when i see it is complete. This is not the most elegant solution. not to mention error prone. Anyone know how to get netcat to close the connection when my gzip command finishes?

thanks!
 
Old 03-12-2010, 12:38 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Chain the commands together?

Code:
netcat -l -p (myport) > backup.sql.gz && <close command>
The && ("and") will run the second command upon the successful completion and exit of the first (if exit status is 0). (Note that I don't know anything about using netcat, so can't add the exact command.)

You can even add on a || "or" option, in case the command fails (exit status >0).

Code:
netcat -l -p (myport) > backup.sql.gz && <close command> || <some other command>
===

Edit: I took a look at the netcat man page, and maybe I was wrong in my understanding. Am I right in understanding that the "listen" option doesn't exit? Well, I see that there is a -q "quit" option that will terminate the connection a set number of seconds after receiving an EOF. Would that do what you want?

Last edited by David the H.; 03-12-2010 at 12:57 AM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash and netcat: Stripping http header Prokke Programming 9 10-05-2009 02:58 AM
Bash Shell Trying to use 'netcat' and 'mkfifio'? Or Whats the best way helptonewbie Programming 5 11-19-2008 09:08 AM
Bash, netcat, redirection and data extraction from stream d1s4st3r Programming 2 11-06-2008 08:51 AM
Netcat s0n|k Linux - Newbie 3 04-16-2006 05:16 PM
Linux vs. Windows DVD predicament (ogle/alsa) Nimoy Linux - Software 8 10-12-2003 08:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:40 AM.

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
Open Source Consulting | Domain Registration