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.
BTConnect.sh
#!/bin/bash
#/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
EOF
/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
BTKill.sh
#!/bin/bash
/usr/bin/pkill mplayer
/usr/bin/bluetoothctl << EOF
disconnect 96:13:5A:00:5D:E6
EOF
/usr/bin/pulseaudio -k
I'm logged in as 'Pi' I run BTConnect.sh manually to play the music in the Music folder and then run BTKill.sh 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:
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):
0,10,20,30,40,50 * * * * /home/pi/BTConnect.sh >>/home/pi/BTConnect.log&
5,15,25,35,45,55 * * * * /home/pi/BTKill.sh >>/home/pi/BTKill.log&
If I log in and run BTConnect.sh 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?