У меня есть простой скрипт для поиска парной bluetooth-гарнитуры, как показано ниже, и он запланирован в cron для запуска каждую минуту. Он работает и подключается к сопряженной bluetooth-гарнитуре, когда она включена, и дает сбой, если устройство bluetooth выключено, как ожидалось. Для отладки планировщика cron ловил логи работы скрипта в лог. И этот журнал добавляет только 0 stdout, а не 1 stderr.
Скрипт
#!/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
Когда я запускаю скрипт вручную, он перехватывает stderr и stdout в зависимости от соединения.
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$
Но в журнале /home/xxxxx/Documents/Shell/scripts/logs/bluetooth.log всегда отображается ниже вывод об успешном подключении независимо от соединения.
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
Может ли кто-нибудь помочь мне, почему в журнале cron нет ошибок, когда Bluetooth не подключается?