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
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
We directly present the script below and then give an explanation
#!/bin/bash while [ 1 ] do 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
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
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.