I'm trying to set up something that telnets into a device and saves the output to file. I can get it working on the CLI, but not running it at boot (using crontab)

This works running running on the command line:

$ nc <host> <port> | tee -a ~/log.txt

The challenge is getting this to run on system boot using cron. nc doesn't run by itself as a cron, so I run it in screen -d -m and it stays open.

screen -d -m nc <host> <port> | tee -a ~/log.txt

There is no output to the file though. I've also tried:

screen -d -m nc <host> <port> 2>&1 | tee -a ~/log.txt

There is still nothing being output to file... I've seen some people recommending the -d tag on nc, but -d appears to be a delay setting on CentOS 7 and not "ignore STDIN" like others are saying.

  • see the daemon xinetd – Rui F Ribeiro Jan 27 '18 at 22:36
  • "nc 3004 > log.txt" or "nc 3004 | tee -a log.txt" work on the cli, butt running in screen, cron, systemd, etc I'm getting no output to the file. Running this creates the file, but there is no text output. – Jonathan Hurst Jan 28 '18 at 4:31

Creating a systemd service worked for me. nc would close immediately when I didn't have the --recv-only tag. This seems to keep it running as intended.


ExecStart=/bin/sh -c 'nc -t --recv-only 3004 >> /home/hursjohn/log.txt'


There are some incomprehensible things in your question.

getting this to run on system boot using cron

If you want to run on boot, you either do some magic with systemd (for Redhat and its followers; Centos has succumbed from vs 7 on) or you put it in rc.local for Unix or Unix-like systems. If you put it in cron (or more exactly: crontab) it will run at regular intervals.

I also do not understand why you would want to wrap nc in a screen command. Though perhaps possible, it seems like unnecessary overkill.

If you want to run at system boot, there is usually not a stdout availabe, so tee is also a bad idea. And, if you want to exit your init-script, you'll want nc to run in the background.

So, I would advice you to put

nohup nc <host> <port> > /home/you/log.txt &

in rc.local and check with sudo systemctl edit --full rc-local that rc.local is run at startup.

  • Vixie cron has the @reboot extension - handy for starting things. – Jasen Jan 27 '18 at 23:25
  • I do like the idea of running this with systemd. I'll explore that avenue as well. I was unable to get your recomendation to work by adding the line "nohup nc 3004 > /home/me/log.txt &" to rc.local. it fails to start. I also tried creading a systemd that runs "/bin/sh -c 'nohup nc 3004 > /home/me/log.txt &" and it runs but exits immediately. also all the output is blank to the file it creates. – Jonathan Hurst Jan 28 '18 at 1:00

Your Answer

