linuxbashshellwatchlsusb

Creating a bash script that logs the output of 'watch lsusb' into an empty file


I have an embedded Linux system (running Ubuntu 10) on a microprocessor that has an onboard USB hub (specifically a BeagleBone Black).

I made a simple bash script that's supposed to run a command, watch lsusb; and as that runs, my program needs to dump the output my command generates into a text or JSON file (ideally on a USB stick, called usb0 for the sake of the example).

So far, this is what I have:

#!/bin/bash
#DATE=$(date +'%F %H:%M:%S')
DIR=/home/ubuntu/lsusb_logs
CMD='watch lsusb'
$CMD > $DIR

This runs until I stop it, which is fine. But when I go to look at my now created lsusb_logs file, all the data appears to be either encoded or needs to be formatted because its not at all like the original format a single lsusb or even watch lsusb outputs.

The purpose of this script is to gather historical data of the peripherals on the USB hub state over a 24 hour period in an environment chamber (climate stress testing).

Any advice or insight is helpful, thanks.


Solution

  • watch is going to print some non-readable characters because it needs to clear the screen every time it runs the command. You could however just run the command in an infinite while loop with some delay:

    while true; do
        lsusb >> lsusb_logs
        sleep 1 # 1 second delay
    done