User avatar
Posts: 44
Joined: Mon Jul 24, 2017 12:19 pm

Bluetoothctl and PulseAudio via Crontab problems

Fri Dec 14, 2018 2:18 pm

I'm trying to get my Pi connected to my Bluetooth speakers to play music at certain times of the day via cron jobs.
The following scripts work if I run them manually in SSH, but when I put them in as cron jobs, it fails.

Code: Select all

#/usr/bin/pulseaudio -D --system
/usr/bin/sudo service bluetooth restart
/bin/sleep 10
#/usr/bin/pactl load-module module-bluetooth-discover
/usr/bin/pactl list sinks short
/bin/sleep 5
/usr/bin/bluetoothctl << EOF
connect 96:13:5A:00:5D:E6
/bin/sleep 15
/usr/bin/pactl set-source-volume 0 65535
/usr/bin/pactl list sinks short
/usr/bin/pactl load-module module-loopback source=0 sink=1 rate=44100 adjust_time=0
/usr/bin/mplayer --volume 35 -shuffle -ao pulse Music

Code: Select all

/usr/bin/pkill mplayer
/usr/bin/bluetoothctl << EOF
disconnect 96:13:5A:00:5D:E6
/usr/bin/pulseaudio -k
I'm logged in as 'Pi'
I run manually to play the music in the Music folder and then run to stop playing.
These are all in my /home/pi folder.
When the above '/usr/bin/pactl load-module module-loopback source=0 sink=1 rate=44100 adjust_time=0' executes, I get:

Code: Select all

0       alsa_output.platform-soc_audio.analog-stereo    module-alsa-card.c      s16le 2ch 44100Hz       SUSPENDED
1       bluez_sink.96_13_5A_00_5D_E6.a2dp_sink  module-bluez5-device.c  s16le 2ch 44100Hz       SUSPENDED
But when it is run from a cron job, the '1 bluez_sink...' does not show up. So mplayer runs and plays the music, but its not sent to out the bluetooth device.
Here's my cron job (created as ''Pi' user, not Root):

Code: Select all

0,10,20,30,40,50 * * * * /home/pi/ >>/home/pi/BTConnect.log&
5,15,25,35,45,55 * * * * /home/pi/ >>/home/pi/BTKill.log&
If I log in and run manually, then the jobs will run properly as long as I'm logged in as 'Pi'. But I need this always work without having to log in if after the Raspberry Pi reboots.

What am I missing here?

Return to “Troubleshooting”