I have a simple script to look for a paired bluetooth headset as below and scheduled it in cron to run every minute. It works and connects to the paired bluetooth headset when it is switched on and fails if bluetooth device is switched off as expected. To debug the cron scheduler I caught the logs of the script in a log. And that log adds only 0 stdout and not 1 stderr.
Script
#!/bin/bash
TIMESTAMP=`date "+%d-%m-%Y %H:%M:%S"`
#rfkill block bluetooth ---Use this to block bluetooth
bluetoothctl power on
if [ $? == 0 ]
then
echo "$TIMESTAMP Bluetooth is started.Connectting to paired device"
bluetoothctl connect 74:45:CE:97:90:72
if [ $? == 1 ]
then
echo "$TIMESTAMP Failed to Connect Sony headset. Please check the headset availability"
bluetoothctl power off
echo "$TIMESTAMP Stopped the Bluetooth"
else
echo "$TIMESTAMP Connected to Sony Headset via Bluetooth"
fi
fi
Cron:
#To Connect Bluetooth automatically
* * * * * /home/xxxxx/Documents/Shell/scripts/bluetooth.sh >> /home/xxxxx/Documents/Shell/scripts/logs/bluetooth.log 2>&1
When I run the script manually it catches stderr and stdout based on the connection.
Changing power on succeeded
23-01-2022 22:12:59 Bluetooth is started.Connectting to paired device
Attempting to connect to 74:45:CE:97:90:72
Failed to connect: org.bluez.Error.Failed
1
23-01-2022 22:12:59 Connected to Sony Headset via Bluetooth
HP-Pavilion:~/Documents/Shell/scripts$
But the log /home/xxxxx/Documents/Shell/scripts/logs/bluetooth.log always has below output as successfully connected irrespective of the connection.
23-01-2022 22:10:01 Bluetooth is started.Connectting to paired device
Attempting to connect to 74:45:CE:97:90:72
0
23-01-2022 22:10:01 Connected to Sony Headset via Bluetooth
23-01-2022 22:11:01 Bluetooth is started.Connectting to paired device
Attempting to connect to 74:45:CE:97:90:72
0
23-01-2022 22:11:01 Connected to Sony Headset via Bluetooth
23-01-2022 22:12:01 Bluetooth is started.Connectting to paired device
Attempting to connect to 74:45:CE:97:90:72
0
23-01-2022 22:12:01 Connected to Sony Headset via Bluetooth
23-01-2022 22:13:01 Bluetooth is started.Connectting to paired device
Attempting to connect to 74:45:CE:97:90:72
0
23-01-2022 22:13:01 Connected to Sony Headset via Bluetooth
23-01-2022 22:14:01 Bluetooth is started.Connectting to paired device
Attempting to connect to 74:45:CE:97:90:72
0
23-01-2022 22:14:01 Connected to Sony Headset via Bluetooth
Can someone help me out why cron's log does not have the errors whenever bluetooth is not getting connected?