Options

Shell Script Question: Detect/Wait for USB Stick to be Ready

RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
I have a POS Linux box, some older version of Open SUSE, and I have a script that unmounts the USB thumb drive, formats it, and then waits for the device to become ready again before it runs a script that does a copy/MD5 checksum verification on the source and destination file to ensure the copy was valid. The problem is that on one box the USB thumb drive does not become ready after the format in a consistent way. I takes anywhere from 1 to 2+ minutes before I can access the drive via /media/LABELNAME.

The direct path is /dev/sdb but, of course, I cannot access it directly via this path to copy the files. Here is my shell script as it stands:
#!/bin/bash
set -e
echo "Starting LABELNAME.\n\nUnmounting /dev/sdb/"
umount /dev/sdb
echo "Formatting /dev/sdb/"
mkfs.vfat -I -F32 -n "LABELNAME" /dev/sdb
echo "Waiting on remount..."
sleep 30
echo "Format complete. Running make master."
perl /home/labelname_master.20120830.pl

Any suggestions?

Comments

  • Options
    UnixGuyUnixGuy Mod Posts: 4,564 Mod
    So for some reason on one of the boxes and format (mkfs) command takes more time? The reason could be anything, the box might be slow or the system is busy doing something else.

    How about giving it more time by increasing the "sleep 30" from 30 seconds to say, 90 seconds (sleep 90) ?
    Certs: GSTRT, GPEN, GCFA, CISM, CRISC, RHCE

    Learn GRC! GRC Mastery : https://grcmastery.com 

  • Options
    dontstopdontstop Member Posts: 579 ■■■■□□□□□□
    #!/bin/bash
    set -e
    echo "Starting LABELNAME.\n\nUnmounting /dev/sdb/"
    umount /dev/sdb
    echo "Formatting /dev/sdb/"
    mkfs.vfat -I -F32 -n "LABELNAME" /dev/sdb
    echo "Waiting on remount..."
    sleep 30
    echo "Format complete. Running make master."
    perl /home/labelname_master.20120830.pl
    

    Any suggestions?

    S_DIR is the Directory that your drive mounts as i.e. /media/LABELNAME

    This will loop until it finds the mounted drive file.
    #!/bin/bash
    
    
    S_DIR="/tmp/hereiam.file"
    
    
    while [ `stat ${S_DIR} 2>/dev/null 1>&2; echo $?` -ne 0 ]
    do
            sleep 1
            echo "Not found yet :)"
    done
    
Sign In or Register to comment.