Reputation: 3
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.
Upvotes: 0
Views: 1322
Reputation: 6621
Instead of looping through the same repetitive command indefinitely, you can take another approach.
You can utilize udev
to monitor for plugged
or unplugged
USB devices, and execute a script at that time.
Example, create 2 scripts:
vi /bin/device_added.sh
vi /bin/device_removed.sh
which will log to a log file the ACTION (added or removed),
and make those executable:
chmod +x /bin/device_added.sh
chmod +x /bin/device_removed.sh
then create a udev rule that will contain the triggers on when a device change is detected:
vi /etc/udev/rules.d/80-test.rules
which will contain for example:
SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_added.sh"
SUBSYSTEM=="usb", ACTION=="remove", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_removed.sh"
This way with your 2 scripts log only upon change, and not all the time..
Upvotes: 1
Reputation: 5789
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
Upvotes: 3