Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm having bash backgrounding and file redirection issues.

I need to attach to a remote sensor box and record the ascii datastream from that box onto disk files. I'd like to break that datastream into segments of about 10 minutes each with a datetime stamp suffixed onto the filename.

To that end, I've got a script that generates a filename, connects to the remote box using nc > filename. (note that I've set the timing in the attached code to 1 minute, rather than 10 minutes).

This script records data to the generated filename as expected:

#!/bin/bash
DEST=/home/sensors/gps1
[[ -d $DEST ]] || mkdir -p $DEST

while true

do

  DESTFILE=$DEST/"gps1-freq-ref-capture-"`date +"%Y-%m-%d-%H%M"`

  nc fepts03 20014 > $DESTFILE
  NCPID=$!

  sleep 60 ; kill $NCPID

done

But the execution never gets past nc.

Backgrounding nc, on the other hand, gives the right filenames, but they are empty files.

 #!/bin/bash
DEST=/home/sensors/gps1
[[ -d $DEST ]] || mkdir -p $DEST

while true

do

  DESTFILE=$DEST/"gps1-freq-ref-capture-"`date +"%Y-%m-%d-%H%M"`

  nc fepts03 20014 > $DESTFILE &     # <-- note backgrounding ampersand
  NCPID=$!

  sleep 60 ; kill $NCPID

done

But the files are empty:

$ ls -la
-rw-rw-rw-+  1 sensors sensors     0 Jul 23 15:00 gps1-freq-ref-capture-2012-07-23-1500
-rw-rw-rw-+  1 sensors sensors     0 Jul 23 15:01 gps1-freq-ref-capture-2012-07-23-1501
-rw-rw-rw-+  1 sensors sensors     0 Jul 23 15:02 gps1-freq-ref-capture-2012-07-23-1502

I thought it was a stdin/stdout console thing so I tried running in a dtach session with the same results: good filenames, no data.

share|improve this question
    
NCPID is a variable, not a file, so you should use kill $NCPID instead. –  chepner Jul 24 '12 at 1:05
    
corrected. thanks. –  user52874 Jul 24 '12 at 16:56
add comment

1 Answer 1

The output files will be empty if there is nothing to connect to on the other end. Check with netstat -ln | grep portnumber on the server. If the other end is another instance of netcat, you need to wait for it to execute after the client exits and wait for the new process to start.

$ cat connect-and-wait.sh
#!/bin/sh

dir=/home/jaroslav/tmp/so/nc-for-5-sec

while true; do
    now=`date +%Y-%m-%d.%H:%M:%S`
    fil=gps1-freq-ref-capture-$now
    out=$dir/$fil

    nc eee.lan 5555 > $out &
    pid=$!

    sleep 5 && kill $pid && echo wrote $out
    #sleep 1;                                  <-- try this 
done

This approach is flawed because you are bound to lose data while reconnecting. If the other end is indeed another instance of nc, then just restarting the server in a shell loop will be problematic (it fails to re-bind some times in my experience). So try keeping one connection and do something with the output instead:

$ cat connect-and-rotate-log.sh
#!/bin/sh

dir=/home/jaroslav/tmp/so/nc-for-5-sec

now=out
fil=gps1-freq-ref-capture-$now
out_=$dir/$fil

nc eee.lan 5555 > $out_ &

while [ -f $out_ ]; do
    now=`date +%Y-%m-%d.%H:%M:%S`
    fil=gps1-freq-ref-capture-$now
    out=$dir/$fil

    sleep 5
    echo wrote $out

    cp $out_ $out
    echo > $out_
done

It's not perfect, but better. Also you may want to check the connection with nmap for example:

nmap eee.lan -p 5555 | grep -q  '^5555.*open' && echo open
share|improve this answer
    
thank you for the reply. this does look a better method than what I was using. turned out the original problem was rendered irrelevant bacause we decided to replace all the boxes (DIGI TS16 serial/IP converters) with something more solid and reliable (Moxa's) rather than continue to try to troubleshoot. Thanks again. –  user52874 Jul 20 '13 at 17:00
add comment

Your Answer

 
discard

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.