Just feel the annoyance and irritation when the CD tray of your computer opens and closes, opens and closes, again and again, again and again, and does not stop until you shut down the computer. We will be writing a tiny shell script with which we would do the above stuff. Although this is very cruel if you apply this to some one, then please be sure to fix it before he/she shoots you with a 50 caliber rifle. So keep on reading to know how to make the simple script to make an infinite CD-ROM tray open close loop in GNU+Linux


[tweetmeme source=”phoxis”]

We will use the command line program eject which is used to eject removable medias from the command line. So you need to have installed this piece of tool. The name of the package containing the tool is “eject” . To install this in Redhat systems execute the following command, which will install the “eject” package and might also install some dependencies.

yum -y install eject

The Script

We directly present the script below and then give an explanation

while [ 1 ]
  eject -T /dev/sr0
done &

And it is as simple as that.

What it does is simple runs an infinite while loop. Inside the loop the eject command is being executed. The -T switch of the eject command, closes the CD-ROM tray if it is ejected and opens if the CD-ROM tray is closed. /dev/sr0 is the device file related to the CD-ROM drive. In the last line the & sends this script/loop to background, and thus the script cannot be terminated by pressing Ctrl + C.

To execute this script first make sure you give the executable permission to the file.Now you can place this in the target user’s autorun directory, or add this script in .bash_profile :). For example for KDE give cdloop.sh executable permission and place the script inside ~/.kde/Autostart .

chmod +x cdloop.sh
cp cdloop.sh ~/.kde/Autostart

The Solution

This madness needs to be stopped. And it could be done easily by killing the sub-shell which has been created by the script. How can we know which is the shell instance which is running this mad script? It is simple to find it with the ps command and then finding the line with the script name as shown below:

ps aux | grep cdloop.sh

The output would be similar as below

Phoxis   19793  0.0  0.0   5744   480 ?        S    12:18   0:00 /bin/sh /home/Phoxis/.kde/Autostart/cdloop.sh
Phoxis   19957  0.0  0.0   5148   732 pts/0    S+   12:19   0:00 grep cdloop

The second line is just the grep process, we are interested with the first line which shows the process information of the mad script. The second column denotes the Process ID of the shell script. So simply kill that. For the above case i killed it the script with, the following command, or simply launch some graphical process manager like ksysguard in KDE and kill it from there

kill 19793

You also get the path of the source of the mad script, so you can instantly remove it. The killing gets more complicated when you do not define the shell parser the the Shebang in the first line. Try removing the Shebang line and executing the script, in this case you could not filter the process with the script name, so you would have to guess which shell instance you need to kill in order to kill the script.


5 thoughts on “CD Tray Open/Close infinite loop

  1. haha when annoying is the matter damn this is real annoying one :P
    But most of the server doesn’t have CD Rom’s

    They only keep when needed

    another best way is restarting the server after certain time :P

  2. I agree with you, but as you can see this is just a fun script. Restarting the server after some time can be done by placing a script in the autorun and sleeping for some time and then executing the reboot command. Although these are not serious ones and just for fun.

  3. Keeping content short and concise may be another way to keep readers attached, I am new and find myself liking this site.

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s