IgnorantGuru's Blog

Linux software, news, and tips

devmon 1.0.5

devmon 1.0.5 adds ––exec-on-unmount and ––exec-on-remove options to allow commands to be executed on these events. (Note that exec-on-unmount may not execute for devices mounted before devmon was started, or for devices which were automounted by devmon during startup.)

For example, to have devmon automatically add and remove drives from traydevice you might use:

devmon --exec-on-drive  "traydevice %f" \
       --exec-on-remove "pkill -f \"traydevice %f\""


Another example: to receive notifications using notify-send:

devmon --exec-on-drive   "notify-send -t 3000 \"Device %f %l has been automounted\"" \
       --exec-on-unmount "notify-send -t 3000 \"Device %f %l has been UNmounted\""   \
       --exec-on-remove  "notify-send -t 3000 \"Device %f has been REMOVED\""


Another example: the following user-contributed script can be used to add icons representing mounted and unmounted devices to any desktop. As currently written, this script requires the icon files drive-hdd-unmount.svg and drive-hdd-mount.svg to be placed in ~/.config/devmon/

#!/bin/bash
dev="$1"
devname=`echo $dev | sed 's/.*\///'`  # strip path from device
if [ "$2" = "" ];then
    label="$devname"
else
    label="$2"
fi
state="$3"
desktopbase="${HOME}/Desktop"
desktopentry="$desktopbase/$devname.desktop"
iconbase="${HOME}/.config/devmon"
echo "CreateDeskIcons: dev='$dev' label='$label' state='$state' "

if [ $state = "-1" ]; then
    if [ -e "$desktopentry" ]; then
        rm "$desktopentry" -rf
    fi
    exit
fi

if [ ! -e "$desktopentry" ]; then
    touch "$desktopentry"
fi

echo "[Desktop Entry]"     > "$desktopentry"
echo "Version=1.0"         >> "$desktopentry"
echo "Type=Application"    >> "$desktopentry"
echo "Encoding=UTF-8"      >> "$desktopentry"

case $state in
    "0" ) # not mounted
        icon="drive-hdd-unmount.svg"
        echo "Name=$label"                 >> "$desktopentry"
        echo "Exec=udisks --mount $dev"    >> "$desktopentry"
        echo "Icon=$iconbase/$icon"        >> "$desktopentry"
        ;;

    "1" ) # mounted
        icon="drive-hdd-mount.svg"
        echo "Name=$label"                 >> "$desktopentry"
        echo "Exec=udisks --unmount $dev"  >> "$desktopentry"
        echo "Icon=$iconbase/$icon"        >> "$desktopentry"
        ;;
    * )
        ;;
esac 

Assuming the above script is installed as “createdeskicons”, devmon can run it with:

devmon --exec-on-drive   "createdeskicons %f %l 1" \
       --exec-on-unmount "createdeskicons %f NoLabel -1" \
       --exec-on-remove  "createdeskicons %f NoLabel -1"


Other changes to devmon 1.0.5: When %f is passed to a command, it is no longer single quoted (%d and %l are still single quoted). Also, devmon will now produce a warning on startup if multiple instances are running (which is not recommended for most purposes).

See previous devmon updates

August 3, 2011 Posted by | Scripts | 5 Comments