5

I'm trying to get my Raspberry Pi which is currently connect to a bluetooth speaker to play an audio file daily on a schedule basis where my cron job is being update @daily to get new timing (It's basically a call to prayer)

crontab -l

@daily /home/pi/athan/update_prayers.sh
39 05 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #fajr
31 12 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #dhuhr
34 15 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #asr
05 18 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #maghrib
24 19 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #isha

So, the above is what I have right now and this is the content of /home/pi/athan/update_prayers.sh

#!/bin/bash
/home/pi/.nvm/versions/node/v7.5.0/bin/node /home/pi/athan/set_prayer.js

Basically my set_prayer.js is just using this https://www.npmjs.com/package/crontab module to set crontab and I was able to get it update daily no problem here. Right now I'm using Mplayer to play the audio and this is the command I use:

/home/pi/athan/call_prayer.sh

#!/bin/bash
/usr/bin/mplayer /home/pi/athan/athan.mp3

My problem here is that when it's time for the cron job to run there's no sound or I don't even know if the job is being run, but when I do it manually I can hear the audio being played no problem. I've also tried to run it directly here using /usr/bin/omxplayer -o alsa /home/pi/athan/athan.mp3 instead of running bash script and it doesn't seem to work with cron but works fine when I run the command directly.

Ali
  • 9,997
  • 20
  • 70
  • 105
  • Hm,, you're using full paths to everything (or did I miss one?), so that eliminates a common problem. Rather than coding for `MM HH DD MM * /path/to/prog` why not just use `MM HH * * * * /path/to/prog` These will run everyday. Good luck. – shellter Feb 27 '17 at 22:47
  • Problem is that the time for each prayers change based on the sunrise/sunset etc so this is just to eliminate any schedule in the file to not run again though I have a script to remove them midnight and update with new one anyway... – Ali Feb 27 '17 at 22:50
  • Well see if having a static crontab makes any difference. If it starts working, then we have something to debug. Also, while it shouldn't matter, remove the `>/dev/null 2>1`s fo test? Good luck. – shellter Feb 27 '17 at 22:51
  • So I just edit crontab using `crontab -e` for one of them to be `54 17 * * * /usr/bin/omxplayer -o alsa /home/pi/athan/athan.mp3` and same story here... :( – Ali Feb 27 '17 at 22:54
  • Arg, better yet, make sure you're not discarding important error info, so do `...> /tmp/athan1.log 2>&1`. If you don't see the files being created, then this tells you that there is something else wrong. Good luck. – shellter Feb 27 '17 at 22:56
  • So, interesting enough the file was created but no log/information in that file at all :/ so I tried to run the command directly again and I can hear the audio being played and can see the message from the player like normal... – Ali Feb 27 '17 at 23:01
  • @shellter another update though... I can see some messages in `mail` where the message is just like the audio was being played but just no sound for some reason? ` Audio codec mp3 channels 2 samplerate 8000 bitspersample 16 Subtitle count: 0, state: off, index: 1, delay: 0 have a nice day ;) ` – Ali Feb 27 '17 at 23:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136797/discussion-between-ali-and-shellter). – Ali Feb 27 '17 at 23:06
  • Are you able to play an audio with your bluetooth speaker? Or is there just an issue playing the audio file with the cron job? – Oliver-H-Miller Feb 28 '17 at 02:57
  • @oliver_H_miler Bluetooth working fine when I play any audio file myself – Ali Feb 28 '17 at 07:23

1 Answers1

23

I was struggling with a similar problem (no audio on Bluetooth speaker if the command is run from crontab.

The problem was the mix pulseaudio/crontab and the environment setup.

The following solution worked for me: https://wiki.archlinux.org/index.php/PulseAudio#Play_sound_from_a_non-interactive_shell_.28systemd_service.2C_cron.29

so basically just do crontab -e and add the following:

XDG_RUNTIME_DIR=/run/user/user_id

the user id can be found out by the following command:

id [user_name]

You can also use this in crontab:

* * * * * XDG_RUNTIME_DIR=/run/user/$(id -u) /path/to/script

it will automatically use current user ID.

16851556
  • 255
  • 3
  • 11
Yacine
  • 346
  • 2
  • 4
  • Thanks! an entire night searching the solution! For the records my error with VLC and alsa was: "xcb_connection_has_error() returned true" Using a custom service with Python in a Raspberry Pi 3, in service I've added: [Service] Environment="XDG_RUNTIME_DIR=/run/user/user_id" – Cristian Deluxe Aug 11 '17 at 05:22
  • Works great! Read it too fast the first time. Keep in mind that the "user_id" is a number, not your user name. – David Mar 25 '19 at 22:14
  • Thank you very much, just what I was looking for. – Zujaj Misbah Khan Mar 23 '21 at 14:53