Will this do?
while ! dd if=/dev/sr2 bs=2048 count=1 of=/dev/null 2>/dev/null; do sleep 1; done
Replace /dev/sr2 with your actual cd/dvd device.
You can make it more robust by checking for errors other than No medium found; untested, since I don't have any broken DVD at hand:
dev=/dev/sr2
while :; do
err=$(dd if=$dev of=/dev/null bs=2048 status=none count=1 2>&1)
case $err in
"dd: failed to open '$dev': No medium found")
sleep 1 ;;
'')
# successfully opened
break ;;
*)
# unexpected error
# play some SOUND in the speakers
# and wait for user input to continue
read wtf ;;
esac
done
Update:
On drives that support it, linux will automatically close the tray on trying to open the device. While this could probably be disabled by ioctl(CDROM_CLEAR_OPTIONS, CDO_AUTO_CLOSE), I wasn't able to find any command line utility implementing it. Since that forces one to use C, better use C for the whole thing.
So, here's a small application that can check and poll for the cd/dvd drive status.
If called with a single argument:
cdstatus /dev/sr1
it will print the status of /dev/sr1: one of no_disc, tray_open, drive_not_ready or disc_ok.
If called with two arguments:
cdstatus /dev/sr1 1.3
it will poll /dev/sr1 every 1.3 seconds until its status is disk_ok.
It could be easily built with cc -Os -Wall cdstatus.c -s -o cdstatus, provided that the gcc and libc6-dev packages are installed.
cdstatus.c
#include <sys/ioctl.h>
#include <linux/cdrom.h>
#include <fcntl.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
int main(int argc, char **argv){
int fd, s; int pt = -1;
if(argc < 2)
errx(1, "usage: %s /dev/srX [poll_secs] [verbose]", argv[0]);
if((fd = open(argv[1], O_RDONLY|O_NONBLOCK)) == -1)
err(1, "open %s", argv[1]);
if(argc > 2 && ((pt = strtod(argv[2], 0) * 1000) < 1 || pt > 3600000))
errx(1, "bad timeout '%s'", argv[2]);
redo:
switch(s = ioctl(fd, CDROM_DRIVE_STATUS, 0)){
case -1: err(1, "ioctl(CDROM_DRIVE_STATUS)");
case CDS_NO_INFO: errx(1, "ioctl(CDROM_DRIVE_STATUS) not implemented");
}
if(pt < 0 || argc > 3)
switch(s){
case CDS_NO_DISC: printf("no_disc\n"); break;
case CDS_TRAY_OPEN: printf("tray_open\n"); break;
case CDS_DRIVE_NOT_READY: printf("drive_not_ready\n"); break;
case CDS_DISC_OK: printf("disc_ok\n"); break;
default: printf("status=%d\n", s); break;
}
if(pt > 0 && s != CDS_DISC_OK){
if(poll(0, 0, pt) < 0) err(1, "poll");
goto redo;
}
return s != CDS_DISC_OK;
}