To run really headless it is necessary to rip CDs whenever they are inserted and eject them afterwards. To achieve this I’ve chosen to use udev. First we create a script which prevents abcde to run multiple times and definitely ejects the cd even if abcde fails because of faulty CDs or any other stupid things which might happen during ripping…
sudo nano /usr/local/sbin/audio-cd-rip.sh
Now the few lines of code
#!/bin/bash LOGFILE=/var/log/cdrip.log echo "$(date)" >> $LOGFILE ( # Wait for lock on /var/lock/.audio-cd-rip.lock (fd 200) for two hours flock -x -w 7200 200 || exit 1 abcde 2>&1 >> $LOGFILE rc=$? if [[ $rc != 0 ]] ; then eject exit $rc fi ) 200>/var/lock/.audio-cd-rip.lock
Why wait two hours? It would wait two hours. But a maximum of two hours. In real life this should never happen.
I saw multiple events from udev when a CD is inserted. To prevent multiple runs of the same script at the same time, I just let one instance of the script’s content (abcde, eject) run and block other requests until it’s done. If one request is waiting longer than two hours it gives up and exits.
Mark the script executable.
sudo chmod +x /usr/local/sbin/audio-cd-rip.sh
Now we create a udev rule that runs this script. A udev rule lets the udev service call our script it the conditions of the rule match.
sudo nano /etc/udev/rules.d/99-cd-audio-processing.rules
Insert one single line:
SUBSYSTEM=="block", KERNEL=="sr0", ACTION=="change", RUN+="/usr/local/sbin/audio-cd-rip.sh &"
This says, that the script shall run when a CD with at least one audio track is inserted in block device sr0 (which is usually your first CDROM/DVD/WHATEVER-Drive). The script will run in background (you see the ‘&’?). Now reload udev rules
sudo udevadm control --reload
And here our mission is completed.
Thanks for watching us.
<Cliffhanger>But, is it? Really? </Cliffhanger>
No, sadly it isn’t over. There is currently a problem on Raspbian with udev to detect manually inserted or ejected media. Don’t know why or what, but got a workaround!
We ‘touch’ our CDROM device every minute by cron. this lets udev wake up and our script runs if a media has been inserted. If ever this bug is fixed, you don’t need this ‘touch’ anymore. On other distros it should run without this trick.
sudo crontab -e
Add this line at the end
*/1 * * * * /bin/touch /dev/sr0
And here our mission is really completed. Thanks for watching us.