Register a domain and help support LQ
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 12-25-2004, 03:39 PM   #1
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 12.04
Posts: 913

Rep: Reputation: 34
Toggling "eject" and "eject -t"

I would like to write a shell script that invokes either "eject" or "eject -t" depending upon the state of my DVD drive tray. I want to place a shortcut of the script so I can click on it to open/close the tray. How can I check the state of the tray from within the script?
#! /bin/sh

if [ <tray_is_closed> ]; then
    eject -t
Old 12-25-2004, 07:07 PM   #2
Registered: Sep 2004
Location: Worcester, MA USA
Distribution: Debian, Gentoo, Fedora FC2
Posts: 66

Rep: Reputation: 15
some pseudocode for ya
write an integer to a file that sets the state of the tray
read open_closed.txt into open
if [ open == 1] 
eject -t 
write 0 to open_closed.txt
write 1 to open_closed.txt
Old 12-25-2004, 10:14 PM   #3
Senior Member
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
there is a way to get information in c
int test = (ioctl(fdcdrom, CDROM_DRIVE_STATUS));

but it's limited like it won't just open an empty drive
try this and look at linux/cdrom.h for the rest of the possible return values
i don't think you will get much farther than this -- i tried once -- but i might be wrong

#include <sys/stat.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <stdlib.h>

int main() {
  int test = 0;
  int fdcdrom = open("/dev/cdrom", O_RDONLY);
  test = (ioctl(fdcdrom, CDROM_DRIVE_STATUS));
// if it's open close it
  if (test == 2) { 
                 ioctl(fdcdrom, CDROMCLOSETRAY);
                 return 0;
 } else {
// i think here it will only eject if there is a disk !
                 ioctl(fdcdrom, CDROMEJECT);
                 return 0;
Old 12-27-2004, 03:01 AM   #4
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 12.04
Posts: 913

Original Poster
Rep: Reputation: 34
Thanks guys!

I looked at both solutions. It seems like the C program is a better fit for me. Not that there's anything wrong with the first one - it's just that writing the status to a file means I always have to use that script to open/close the tray in order to keep the file content in sync with the actual state of the tray. The C program seems more flexible. I'll try that and see how it goes.

Again, I appreciate both you guys help!


[EDIT] The C program worked beautifully. The only thing I had to fix was the parameter to the open function. You have to specify O_NONBLOCK in addition to O_RDONLY in order for the ioctl function to work. I'm a happy man!

Last edited by daihard; 12-27-2004 at 03:34 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
eject -v says "unable to open /dev/hdc" Simon Bridge Slackware 2 11-05-2005 01:55 AM
"Enhanced" CDs refusing to eject. Tyke Fedora 3 04-09-2005 07:25 AM
JDS "Live CD" eject? Ryptyde JDS 3 11-02-2004 09:15 AM
CD Won't Eject With "eject" Command Dr. Ephemeron Slackware 13 11-13-2003 12:27 AM

All times are GMT -5. The time now is 08:07 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration