What I want to do : at boot, start a playlist on a headless RBPi A+ through the headphones jack using mpg123.

I Wrote a shell script targeting mpg123:

# see if (and when) my script was run : 
date >> /home/MY_USER_NAME/date.txt
# infinitely and randomly play a playlist using mpg123
/bin/mpg123 -v -o alsa --mono -Z 0 /home/MY_USER_NAME/music/lofi-grl/*.mp3

and added this line to crontab using sudo crontab -e : @reboot sh /home/MY_USER_NAME/start.sh >> ~/cron.log 2>&1

In short, the script is working fine from the terminal, but partially when run by cron : the file date.txt is there, but there is no sound, and if I do top | grep mpg123 there is no mpg123 process.

Everything in the cron.log file is fine except the last line :

000000+143478  00:00.00+01:02:27 --- 100=100  32 kb/s  104 B acc    0 clip p+0.000^M> 000008+143470  00:00.18+01:02:27 --- 100=100  32 kb/s  104 B acc    0 clip p+0.000
main: [src/mpg123.c:839] error: Deep trouble! Cannot flush to my output anymore!

I tried using crontab -e instead of sudo crontab -e, didn't work. I tried adding mpg123 to sudoers ALL ALL = NOPASSWD: /usr/bin/mpg123 to sudoers like suggested in an other thread but nothing works. I even tried using /etc/rc.local which didn't work either. I am really pulling my hair on this one ! Please help !

1 Answer 1


I (finally) figured out : 1/ Although you can omit it when using the terminal it You have to specify which device you want mpg123 to use in the form -a hw:x,y when run from cron task (in my case hw:1,0 which is headphones jack output o, the RBPi)

2/ mpg123 can't operate until the sound device is ready (obviously) which is not the case when the @reboot event is fired. the solution was to wait for 20 seconds before running the script : @reboot sleep 20 && sh /path/to/myscript.sh

Now I have to figure out how to run this script exactly the moment the audio device is ready.

  • "have to figure out how to run this script exactly the moment the audio device is ready". You won't be able to do that with cron - it's not "resource aware". You might be able to cobble a systemd unit, but be preared to spend time on this!
    – Seamus
    Commented Jan 18 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.