0

I have a set of commands that I am attempting to run in a script. To be exact, the lines are

rm tmp_pipe
mkfifo tmp_pipe
python listen_pipe.py &
while [ true ]; do nc -l -w30 7036 >>tmp_pipe; done &

listen_pipe.py is simply

if __name__ == "__main__":
    f = open("tmp_pipe")

    vals = " "

    while "END" not in vals:
        vals = f.readline()
        if len(vals) > 0:
            print(vals)
        else:
            f = open("tmp_pipe")

If I run the commands in the order shown I get my desired output, which is a connection to an ESP device that streams motion data. The connection resets after 30 seconds if the ESP device leaves the network range or if the device is turned off. The python script continues to read from the pipe and does not terminate when the tcp connection is reset. However, if I run this code inside a script file nc fails to connect and the device remains in an unconnected state indefinitely. The script is just

#!/bin/bash
rm tmp_pipe
mkfifo tmp_pipe
python listen_pipe.py &
while [ true ]; do nc -l -w30 7036 >>tmp_pipe; done &

This is being run on Ubuntu 16.04. Any suggestions are greatly welcomed, I have been fighting with this code all day. Thanks,

Ian

ihunter2839
  • 77
  • 1
  • 2
  • 14
  • FYI, `nc -l` ignores the -w parameter (https://man.openbsd.org/nc.1). – Nic3500 Dec 27 '17 at 03:52
  • 3
    Side note: `while [ true ]` tests if the string `true` is non-zero. You probably meant `while true` or the equivalent `while :`. Wouldn't change anything, just pointing it out. – Benjamin W. Dec 27 '17 at 04:41
  • @Nic3500 It is not totally ignored. -l makes netcat listen forever to establish the connection, but the -w is still relevant to idle connections. – ihunter2839 Dec 27 '17 at 06:20

0 Answers0