Safely removing a USB drive with a bash script

In a previous post (Safely removing a USB flash drive) I noted that, when removing an external USB drive, Linux Mint 18.2 (Sonya) provided a message indicating that “It is now safe to remove the drive”. However, I sometimes noticed that, even though this message was displayed, the red LED on my USB flash drive was still flashing, indicating that disk activity was still underway such that it wasn’t necessarily safe to pull the plug. Furthermore, the safe-to-remove message does not occur in Linux Mint 18.3 (Sylvia) – perhaps because it didn’t really work?

This led me to revisit the idea of a bash script that would wait for any cached data to be written to the USB drive before indicating that it was indeed safe to remove the external drive. I think I have now created such a bash script that has the desired effect.

The script undertakes multiple tasks. Firstly, it calls the sync command to ensure that any disk writes are complete before proceeding. Sync doesn’t return control to the main script file until any/all cached data have been written to the USB drive. Secondly, a combination of lsblk and grep is used to identify the device name for the USB drive of interest. This is necessary since if multiple USB devices are plugged into the computer, the device name (e.g. /dev/sg1) may not always be the same. The last three characters in the device name (e.g. sg1) are saved to a “usbname” variable for use in unmounting and powering off the USB drive. Finally, the udisksctl command is used to unmount the USB drive and then power it off.

The specific code used is as follows:

#! /bin/bash

# Eject USB drive once buffer transfer has completed

echo "Flushing USB drive buffer"

echo "sync complete"

# Identify the device name for the SILICON16GB USB drive

usblongname=$(lsblk -l | grep SILICON16GB)

# Unmount the USB drive

udisksctl unmount -b /dev/$usbname

echo "unmount complete"

# Power off the USB drive

udisksctl power-off -b /dev/$usbname

echo "power off complete"

# Script complete

echo "Shell command complete"

Note that the above-noted script has the name of my USB flash drive hard coded as “SILICON16GB“. This designator should be changed to suit a different user’s system. Also, udisksctl is part of the gnome-disk-utility package so this package should be installed if it is not available by default.


Safely removing a USB flash drive

Eject / safely remove vs umount

wait for dd command to fully write to the disk

sync(8) – Linux man page

ubuntu manuals – udisksctl – The udisks command line tool

gnome-disk-utility package in Ubuntu

This entry was posted in Bash script, Mint and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s