Question

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.

Était-ce utile?

La solution

Likely the environment is different when running under cron.

Try qualifying the exact path(s)

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

Also note that there are two "competing" popular versions of netcat:

  • netcat-traditional
  • netcat-openbsd

with slightly different command line options

Also, consider just making things a loop:

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

while true
do
     nc -dl 12.34.56.78 1234
done
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top