Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So, I have device on my network which sits there listening on a port for a connection, and when a connection is made it dumps ascii data out. I need to capture that data to a file. I wrote a dead simple shell script that does this:


#Config Variables. Age is in Days.
FILENAME=data_`date +%F`.dat

#Sanity Checks
if [ ! -e $DATA_ROOT ]
        echo "The directory $DATA_ROOT seems to not exist. Please create it."
        exit 1

        echo "You seem to have extracted data already today. Aborting"
        exit 1

#Get Data
#Compress old Data
find $DATA_ROOT -type f -mtime +$COMPRESS_AGE -exec gzip {} \;

exit 0

It works great when I run it by hand, but when I run it from cron, it doesn't capture any of the output. If I replace nc with telnet I see the initial telnet headers about escape sequences and whatnot, but not the data.

Ideas? I've tried forcing bash to act like an interactive shell with -i. I've tried redirecting both stderr and stdout. I know it's got to be some silly simple thing, but I'm utterly failing. This is driving me nuts...

EDIT I also just noticed that the nc processes from all my previous attempts at this have been siting sleeping, and when I killed them, cron sent me a bunch of non-sensical error messages. At least now I have something to dig into!

share|improve this question
Check that all paths are set correctly. Whenever I've had something that works not work in cron, it's because cron doesn't inherit the correct PATH(s). – Cory J Jul 2 '10 at 22:28
Under which account is your cron running? You are sure it has permissions to the location where it is writing? – Zoredache Jul 2 '10 at 22:38
Add this line immediately before the nc line: echo 'Starting' > $DATA_ROOT/$FILENAME.test as a test and see if you get that line in the file with ".test" appended to the expected name. (Also, does the file you expect get created? Is it zero bytes long? What happens if you specify the full path to nc such as /bin/nc $HOST 2022 ... or wherever it's located on your system - find out by doing type -a nc?). – Dennis Williamson Jul 2 '10 at 23:16
Cory J - The PATH is correct and/or I'm using absolute paths in the script. Zoredache - Everything is owned by root, and it is being run out of root's crontab Dennis - Adding an "echo starting...." and that does get captured in the file. The file does get created, even without that, but it only has a few bogus characters in it. Specifying the full path doesn't change anything. Thanks for the input! Anything else? – qhartman Jul 6 '10 at 15:44

It's possible that your script is working, and it's just the output that's getting lost. You could try capturing the nc output into a variable of it's own, and then echoing that variable out to

#Get Data
ncoutput=$( /path/to/nc $HOST 2202 2>&1 )
printf "%s\n" "$ncoutput" > $DATA_ROOT/$FILENAME
#Compress old Data
find $DATA_ROOT -type f -mtime +$COMPRESS_AGE -exec gzip {} \;

exit 0

printf preserves the formating of the output, whereas echo doesn't.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.