[aklug] linux bash backgrounding and I/O redirection

From: techno curmudgeon <technocurmudgeon@gmail.com>
Date: Mon Jul 23 2012 - 15:48:54 AKDT

Hey folks,

It's been a while since I've done much scripting, and my bash-fu is
embarrassingly lacking now. Anyway, if someone can give me a pointer
I'd appreciate it.

I'm having bash backgrounding and file redirection issues. I expect
it's something simple and obvious, and I'm ok with that 8).

OVERVIEW:

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 bash script generates a filename, connects to
the remote box using nc, and > to the 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 `cat $NCPID`

done

------------------------------

But of course execution never gets past the nc

Backgrounding the 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 `cat $NCPID`

done

------------------------------

$ 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
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:03
gps1-freq-ref-capture-2012-07-23-1503
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:04
gps1-freq-ref-capture-2012-07-23-1504
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:05
gps1-freq-ref-capture-2012-07-23-1505
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:06
gps1-freq-ref-capture-2012-07-23-1506
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:07
gps1-freq-ref-capture-2012-07-23-1507
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:08
gps1-freq-ref-capture-2012-07-23-1508
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:09
gps1-freq-ref-capture-2012-07-23-1509
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:10
gps1-freq-ref-capture-2012-07-23-1510
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:11
gps1-freq-ref-capture-2012-07-23-1511
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:12
gps1-freq-ref-capture-2012-07-23-1512
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:13
gps1-freq-ref-capture-2012-07-23-1513
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:14
gps1-freq-ref-capture-2012-07-23-1514
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:15
gps1-freq-ref-capture-2012-07-23-1515
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:16
gps1-freq-ref-capture-2012-07-23-1516
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:17
gps1-freq-ref-capture-2012-07-23-1517
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:18
gps1-freq-ref-capture-2012-07-23-1518
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:19
gps1-freq-ref-capture-2012-07-23-1519
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:20
gps1-freq-ref-capture-2012-07-23-1520
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:21
gps1-freq-ref-capture-2012-07-23-1521
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:22
gps1-freq-ref-capture-2012-07-23-1522
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:23
gps1-freq-ref-capture-2012-07-23-1523
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:24
gps1-freq-ref-capture-2012-07-23-1524
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:25
gps1-freq-ref-capture-2012-07-23-1525
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:26
gps1-freq-ref-capture-2012-07-23-1526
-rw-rw-rw-+ 1 sensors sensors 0 Jul 23 15:27
gps1-freq-ref-capture-2012-07-23-1527

-------------------------------------

Thinking it was a stdin/stdout console thing, I tried running in a
dtach session . Same results, good filenames, no data.

Any thoughts?

Thanks!
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Mon Jul 23 15:49:03 2012

This archive was generated by hypermail 2.1.8 : Mon Jul 23 2012 - 15:49:03 AKDT