0

I'm using the following /etc/init/jsonlog.conf upstart job:

description     "jsonlog"

start on runlevel [23]
stop on runlevel [06]

respawn

script
  cd /tmp
  echo "about to listen"
  /bin/nc -l 3333
  echo "finished listening"
end script

post-stop script
  sleep 1
end script

The problem is that nc exits immediately every time a client connects.

The required behaviour is that all data received (which is utf8 json) should end up in /var/log/upstart/jsonlog.log. This server is running Ubuntu 12.04LTS.

The nc command works correctly when run from a bash shell.

I'm making an assumption that this be something to do with stdin. I've tried using a -q -1 option, but it hasn't helped.

P.S. I'm fine with the fact that this will listen to only one client at a time.

fadedbee
  • 1,988
  • 4
  • 22
  • 33

2 Answers2

3

I see two things going wrong with your nc command. You are invoking nc in a way intended for two way communication and expect it to perform one way communication.

The -d flag can stop nc from reading from stdin which will get it to do the one-way communication which it appears that you are trying to do.

The other problem is that nc -l will by default only service a single connection and then terminate. You can use the -k flag to change that behavior.

In all the command thus becomes:

nc -dkl 3333

It does however seem you are using a tool not designed for the job. Rather than using nc I would recommend that you use the syslog protocol and a logger designed to work with that protocol.

kasperd
  • 29,894
  • 16
  • 72
  • 122
2

nc -dl is what you need for the BSD flavor of nc.

The nc that comes with RHEL7 is not the BSD version. Instead it's ncat from the nmap software. Unlike the BSD version, there is no -d flag. Instead of -dl, you can use -l --recv-only < /dev/zero. The version of NCAT that comes with EL7 has a (closed/fixed) bug that will close the program before finishing reading from the socket, if EOF is returned from reading stdin. That's why you need to pass data into stdin via '/dev/zero' or something else. The newer versions of NCAT support --no-shutdown which can be used in place of < /dev/zero.