PDA

View Full Version : HOWTO: Unmount & eject CD-Rom with a launcher on panel


Sam
July 28th, 2005, 09:43 PM
This is annoying to get back to the desktop and right-clic on the CD-Rom icon to eject it. I wrote this script to make it simpler !
Create a new file:
$ sudo gedit /usr/local/bin/eject_cd
Paste the following lines:
#! /bin/sh

#
# Try to unmount a CD-Rom device, then eject it.
#

DEVICE="$1"
ZENITY_BIN="/usr/bin/zenity"


#Ctrl-C trapping
trap ctrlc INT
ctrlc()
{
echo -e "\nAborted by user."
rm -rf $TMP_DIR
exit 2
}


#Show a dialog with zenity
#@param string The text to display
show_dialog()
{
if [ "$use_zenity" -gt "0" ] ; then
zenity --error --title "CD-Rom eject" --info-text "$1"
fi
}


#Get parameters
if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then
echo "Usage: eject_cdrom [-q] DEVICE"
echo -e "Try to unmount DEVICE then eject it if successful.\n"
echo "Possible parameters:"
echo -e "-h, --help\tdisplay this help and exit."
echo -e "-z, --zenity\tuse zenity to displays errors in dialog windows."
exit 0
fi

if [ "$1" == "-z" ] || [ "$1" == "--zenity" ] ; then
if [ ! -x "$ZENITY_BIN" ] ; then
echo "You must install zenity before that."
exit 1
fi
use_zenity="1"

device="$2"
else
use_zenity="0"
device="$1"
fi


#Device check
#TODO: Check if DEVICE is truly a device.
if [ ! -e "$device" ] ; then
echo "Parameter DEVICE is not a file."
exit 1
fi


echo "Trying to eject CD-Rom..."

#Unmount
umount "$device" 2>/dev/null
last_err="$?"

if [ "$last_err" -eq "1" ] ; then
msg="Cannot unmount device $device (busy)."
echo "$msg"
show_dialog "$msg"
exit 1
fi

#Eject
eject "$device"
last_err="$?"

if [ "$last_err" -ne "0" ] ; then
msg="Cannot eject device."
echo "$msg"
show_dialog "$msg"
exit 1
fi

exit 0
Make the script executable:
$ sudo chmod +x /usr/local/bin/eject_cd
Create a new launcher on a panel (or wherever you want):
Right-clic on the panel
'Add to Panel'
'Custom Application Launcher'
Type this (replace /dev/cdrom with your CD-Rom device): Name: Eject CD-Rom
Comment: Unmount and eject the CD-Rom /dev/cdrom
Command: /usr/local/bin/eject_cd -z /dev/cdrom
Icon: /usr/share/icons/gnome/24x24/devices/gnome-dev-removable.png
Clic 'Close'
Done !


Note:
This script uses zenity to display errors. However if you don't want this feature, remove the '-z' parameter.

To install zenity:
$ sudo apt-get install zenity

rwabel
July 29th, 2005, 05:46 AM
This is annoying to get back to the desktop and right-clic on the CD-Rom icon to eject it. I wrote this script to make it simpler !
Create a new file:
$ sudo gedit /usr/local/bin/eject_cd
Paste the following lines:
#! /bin/sh

#
# Try to unmount a CD-Rom device, then eject it.
#

DEVICE="$1"
ZENITY_BIN="/usr/bin/zenity"


#Ctrl-C trapping
trap ctrlc INT
ctrlc()
{
echo -e "\nAborted by user."
rm -rf $TMP_DIR
exit 2
}


#Show a dialog with zenity
#@param string The text to display
show_dialog()
{
if [ "$use_zenity" -gt "0" ] ; then
zenity --error --title "CD-Rom eject" --info-text "$1"
fi
}


#Get parameters
if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then
echo "Usage: eject_cdrom [-q] DEVICE"
echo -e "Try to unmount DEVICE then eject it if successful.\n"
echo "Possible parameters:"
echo -e "-h, --help\tdisplay this help and exit."
echo -e "-z, --zenity\tuse zenity to displays errors in dialog windows."
exit 0
fi

if [ "$1" == "-z" ] || [ "$1" == "--zenity" ] ; then
if [ ! -x "$ZENITY_BIN" ] ; then
echo "You must install zenity before that."
exit 1
fi
use_zenity="1"

device="$2"
else
use_zenity="0"
device="$1"
fi


#Device check
#TODO: Check if DEVICE is truly a device.
if [ ! -e "$device" ] ; then
echo "Parameter DEVICE is not a file."
exit 1
fi


echo "Trying to eject CD-Rom..."

#Unmount
umount "$device" 2>/dev/null
last_err="$?"

if [ "$last_err" -eq "1" ] ; then
msg="Cannot unmount device $device (busy)."
echo "$msg"
show_dialog "$msg"
exit 1
fi

#Eject
eject "$device"
last_err="$?"

if [ "$last_err" -ne "0" ] ; then
msg="Cannot eject device."
echo "$msg"
show_dialog "$msg"
exit 1
fi

exit 0
Make the script executable:
$ sudo chmod +x /usr/local/bin/eject_cd
Create a new launcher on a panel (or wherever you want):
Right-clic on the panel
'Add to Panel'
'Custom Application Launcher'
Type this (replace /dev/cdrom with your CD-Rom device): Name: Eject CD-Rom
Comment: Unmount and eject the CD-Rom /dev/cdrom
Command: /usr/local/bin/eject_cd -z /dev/cdrom
Icon: /usr/share/icons/gnome/24x24/devices/gnome-dev-removable.png
Clic 'Close'
Done !


Note:
This script uses zenity to display errors. However if you don't want this feature, remove the '-z' parameter.

To install zenity:
$ sudo apt-get install zenity
sweet, thanks

f76
July 29th, 2005, 05:54 AM
How about adding a few lines in the script to temporarily turn of Nautilus annoying automatic file updating feature that hogs the mount while ejecting?
That would maybe be a usefull allbeit dirty feature.

Ill try your script tonight!/thnx

rwabel
July 29th, 2005, 06:00 AM
How about adding a few lines in the script to temporarily turn of Nautilus annoying automatic file updating feature that hogs the mount while ejecting?
That would maybe be a usefull allbeit dirty feature.

Ill try your script tonight!/thnx
didn't know about the automatic file updating.. does it from time to time check the cd if files have been changed? that would be stupid, but explain why sometimes it takes longer to eject

f76
July 29th, 2005, 06:09 AM
Yes sir. According to some threads it also explains why i cant eject CDs after viewing them(or the properties) in nautilus.
Nautilus somehow falls in love with my cd and wont let her go.. [-X Or maybe thats the famd-- i dont know really.

ritger
July 29th, 2005, 07:20 AM
With me it's gam_server that loves the cd's (and some of the harddrives too). You could fix this by doing a killall -9 gam_server in the script, before the unmount. It instantly respawns, but lets go of the mounted drives (the icon stays on the desktop though, go figure).

gammyhand
August 22nd, 2005, 04:41 PM
With me it's gam_server that loves the cd's (and some of the harddrives too). You could fix this by doing a killall -9 gam_server in the script, before the unmount. It instantly respawns, but lets go of the mounted drives (the icon stays on the desktop though, go figure).
It won't let me save the eject_cd file.

marion
August 29th, 2005, 10:20 PM
Thanks for the script. I was having issue with ejecting the DVD from xine, and now I just close xine and use your script.

Thanks much.

DemiUrge8
August 30th, 2005, 09:13 AM
Is there any way to make the script load the cd tray if you press it again?

Sam
August 30th, 2005, 01:47 PM
Is there any way to make the script load the cd tray if you press it again?
It would be a bit complicated. The 'eject -t' command which closes de cd tray exits with the same status if it closes the tray or if it's already closed. So it won't be easy to detect which action to perform (open or close)...

But you can put an another launcher beside to do this !

hubbadub
October 20th, 2005, 03:50 PM
Thanks, worked like a charm for me

blue_chili
September 15th, 2006, 03:50 AM
Fantastic! Worked first time.

naborcoj
June 17th, 2007, 06:57 PM
Thanks. It's helped me a lot!
But I had to adjust some things on this script to make it run error free on my system. Maybe it's related to the version of bash and zenity, as my pc runs Ubuntu 7.04 Feisty.
What I had to change:

1. zenity syntax


Original code:
zenity --error --title "CD-Rom eject" --info-text "$1"

Changed code:
zenity --error --title="CD-Rom eject" --text="$1"


2. bash test syntax (it didn't work with ==, but just with a single =)


Original code:
if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then

Changed code:
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then



Original code:
if [ "$1" == "-z" ] || [ "$1" == "--zenity" ] ; then

Changed code:
if [ "$1" = "-z" ] || [ "$1" = "--zenity" ] ; then


I found this script on https://help.ubuntu.com/community/EjectCDLauncher but was unable to report those suggestion there. If you can, please include those changes there, so it can be used by others. It took me some time checking the script until I found what to do.

Thanks again!

vbmds
July 18th, 2007, 03:56 AM
Sorry to say that it did not work for me with my laptop. It did unmount but did not eject. I also did not get any zenity notification(s) either.

gregorvm
December 26th, 2009, 03:45 PM
Thank you, it made eject-button on my dell studio-xps really work. Before it only functioned if the disk was unmounted already.

Gregor
Ubuntu Studio on Dell Studio-xps