2

I am listening to a TCP port using netcat, I am receiving a line of comma-deliminated data every 5 minutes and writing it to a text file. Here is the command I'm running:

nc -dl 12.34.56.78 1234 > path/to/my/file & echo $!

When this command is ran via the command line the data is saved to the file and everything is fine.

However, I created a little PHP script that reinstates the netcat listener (using the same command as above) in the event the process stops for whatever reason. The script is executed via a cron job. When this happens, the data is still written to the file however it is always prepended by a string of "^@" characters, the length of which increases each time (every 5 minutes).

Any insight into why this is happening and how to stop it is greatly appreciated!

Matt.

4

1 回答 1

1

在 cron 下运行时,环境可能有所不同。

尝试限定确切的路径

/usr/bin/nc -dl 12.34.56.78 1234 > /absolute/path/to/my/file & echo $!

另请注意,netcat 有两个“相互竞争”的流行版本:

  • netcat-传统的
  • netcat-openbsd

命令行选项略有不同

另外,考虑让事情成为一个循环:

#!/bin/sh
exec < /dev/null > /dev/null

while true
do
     nc -dl 12.34.56.78 1234
done
于 2013-07-23T17:49:48.800 回答