Ubuntu – Help using crontab to play a sound

cronsound

I want to use crontab to play recordings of the big Ben every hour and at 15,30 and 45 minutes.

So far I'm using mplayer2 and the following tasks:

0 1,13 * * * mplayer -really-quiet ~/bin/bigben/Clock_Chime_01.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_75
0 2,14 * * * mplayer -really-quiet ~/bin/bigben/Clock_Chime_02.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_76
0 3,15 * * * mplayer -really-quiet  ~/bin/bigben/Clock_Chime_03.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_77
0 4,16 * * * mplayer -really-quiet  ~/bin/bigben/Clock_Chime_04.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_78
0 5,17 * * * mplayer -really-quiet  ~/bin/bigben/Clock_Chime_05.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_79
0 6,18 * * * mplayer -really-quiet  ~/bin/bigben/Clock_Chime_06.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_80
0 7,19 * * * mplayer -really-quiet  ~/bin/bigben/Clock_Chime_07.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_81
0 8,20 * * * mplayer -really-quiet  ~/bin/bigben/Clock_Chime_08.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_82
0 9,21 * * * mplayer -really-quiet  ~/bin/bigben/Clock_Chime_09.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_83
0 10,22 * * * mplayer -really-quiet  ~/bin/bigben/Clock_Chime_10.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_84
0 11,23 * * * mplayer -really-quiet ~/bin/bigben/Clock_Chime_11.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_85
0 0,12 * * * mplayer -really-quiet ~/bin/bigben/Clock_Chime_12.ogg -volume 100 >/dev/null 2>&1 # JOB_ID_86
15 0-23 * * * mplayer -really-quiet ~/bin/bigben/Clock_Chime_15-1.ogg -volume 100 >/dev/null 2>&1
30 0-23 * * * mplayer -really-quiet ~/bin/bigben/Clock_Chime_15-2.ogg -volume 100 >/dev/null 2>&1
45 0-23 * * * mplayer -really-quiet ~/bin/bigben    /Clock_Chime_15-3.ogg -volume 100 >/dev/null 2>&1

But most of the time it does not work. Sometimes it does play the sound but delayed. ie. supposed to play at 21:15, and it didn't at 21:17 I opened a terminal and played another file with mplayer, after it finished, I heard the one supposed to play 2 min earlier.

Edit:
Now trying with:
* * * * * "/usr/bin/mplayer -volume 100 --ao=pulse /home/myuser/bin/bigben/Clock_Chime_15-1.ogg" without success.

I do have the feeling like the task stays in some kind of cue.

PS. I can share the sound files.

Best Answer

As per this answer: Can I use cron to chime at top of hour like a grandfather clock? you need to export an environment variable before playing sounds in your cron script:

export XDG_RUNTIME_DIR="/run/user/1000"

So rather than calling your music player directly, call a script name in crontab like this:

0 1,13 * * * /home/ME/bin/big-ben.sh /home/ME/bin/bigben/Clock_Chime_01.ogg >/dev/null 2>&1 # JOB_ID_75
    (... SNIP ...)
45 0-23 * * * /home/ME/bin/big-ben.sh /home/ME/bin/bigben/Clock_Chime_15-3.ogg >/dev/null 2>&1
  • Replace ME with your user name.
  • You had an error (the spaces after /bigben and before /Clock on in the last line of your crontab. I've fixed the error above.
  • You were using ~/ shortcut for /home/ME which cron doesn't like. Use full path names in cron.

Then create your /usr/local/bin/big-ben.sh script:

#!/bin/bash
export XDG_RUNTIME_DIR="/run/user/1000"
mplayer -really-quiet "$1" -volume 100
  • "$1" is the parameter 1 passed by crontab entry to your script

Remember to mark the script executable by using:

chmod a+x /usr/local/bin/big-ben.sh
Related Question