views:

335

answers:

2

I've got a device that I'm operating next to my PC and as it runs it's spitting log lines out it's serial port. I have this wired to my PC and I can see the log lines fine if I'm using either minicom or something like:

ttylog -b 115200 -d /dev/ttyS0

I want to write 5 seconds of the device serial output to a temp file (or assign it to a variable) and then later grep that file for keywords that will let me know how the device is operating. I've already tried redirecting the output to a file while running the command in the background, and then sleeping 5 seconds and killing the process, but the log lines never get written to my temp file. Example:

touch tempFile
ttylog -b 115200 -d /dev/ttyS0 >> tempFile &
serialPID=$!
sleep 5
#kill ${serialPID} #does not work, gets wrong PID
killall ttylog
cat tempFile

The file gets created but never filled with any data. I can also replace the ttylog line with:

ttylog -b 115200 -d /dev/ttyS0 |tee -a tempFile & 

In neither case do I ever see any log lines logged to stdout or the log file unless I have multiple versions of ttylog running by mistake (see commented out line, D'oh).

I have no idea what's going on here. It seems to be a failure of redirection within my script.

Am I on the right track? Is there a better way to sample 5 seconds of the serial port?

+2  A: 

It sounds like maybe ttylog is buffering its output. Have you tried running it with -f or --flush?

David Norman
-f is not a valid flag for ttylog, or perhaps I don't understand your question?
pica
I was just looking at the source from http://rocasa.org/ttylog
David Norman
D'oh. `strings /usr/sbin/ttylog` show the -f option as available, but it's not in the man page. I'll give it a try.
pica
That seemed to do the trick, at least in my sample code where I cat to stdout instead of grep things. Thanks, now I can integrate this in with the uber-script.
pica
A: 

You might try the unbuffer script that comes with expect.

Dennis Williamson
will check this out shortly. It looks promising. I did, however find an easier solution in the -f option suggested by David Norman.
pica