Howto create a permanent client connection with netcat?
Solution 1
Two separate processes: One that copies result.txt to netcat. Result.txt is fed via another process.
echo -n >result.txt
tail -f result.txt | nc ip port &
while true
do
read_folder()
process_data() > result.txt
wait 10 sec
done
Solution 2
Just put the redirection outside the loop.
while true; do
read_folder
process_data
sleep 10
done | netcat $ip $port
If you need to output to the loop's standard output from inside the loop, divert it through another file descriptor.
{
while true; do
read_folder
echo tick >&3
process_data
sleep 10
done | netcat $ip $port
} 3>&1
Related videos on Youtube
jmbouffard
Updated on September 18, 2022Comments
-
jmbouffard almost 2 years
I'm writing a bash script that constantly read a folder in a loop to send data to a server at a defined time interval. I'm using netcat as the tool to connect to the server and send the data. My pseudo code would look like:
while true do read_folder() process_data() > result.txt cat result.txt > netcat ip port wait 10 sec done
My only problem is that in the scenario the client connects and disconnects the TCP/IP connection to the server every time. I would prefer to establish the connection at the start of the script and close it at the end.
Is there any way this could be done with command line tools in a bash script?
-
bbaja42 almost 13 yearsActually, I prefer your current setup. In case of long connection, there is bigger chance of connection loss. This way, in case of connection loss, there is automatic retry after 10 seconds
-
BrettRobi almost 13 yearsTake a look at
socat
, it's a netcat replacement that has a built-in functionality to respawn pipes after they've finished.
-
-
jmbouffard almost 13 yearsI thought about something like that but the first process will return when it reaches the end of the file, it will not wait for the other process to write to it.
-
mm2001 almost 13 years@jmbouffard: fixed.
tail -f
will continue to read the file even after it reaches EOF. -
jmbouffard almost 13 yearsThanks it works amazingly well with tail -f. I'm even able to use a fifo pipe instead of a "growing" result.txt file for the transfer. Answer accepted.
-
tcoolspy almost 13 years@Kevim: The edit @Gilles made using
: >result.txt
was fine, it does not leave a newline in the file. Test it yourself. -
Gilles 'SO- stop being evil' almost 13 years
echo -n
is fine too, as long as your shell supports it (some shells print-n
) — so:
is preferable (it works everywhere). And you do needtail -c +1
; without it, iftail
is slow to start, it will omit all but 10 lines of what's already in the file.