我有一个简单的脚本来寻找一个配对的蓝牙耳机,如下所示,并安排它在cron中运行每分钟。它工作并连接到配对的蓝牙耳机,当它被打开时,如果蓝牙设备按预期关闭,它就会失败。为了调试cron调度程序,我在日志中捕获了脚本的日志。该日志只添加0标准输出,而不添加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的日志就没有错误吗?
发布于 2022-01-23 17:56:08
首先,即使您的手动运行也没有给出预期的结果。该设备未能连接,最终的输出应该是“没有连接到索尼耳机”。
选项1:问题在于"echo $?“。删除这个语句,你就会没事的。
选项2:回显$?总是计算为0,因此如果总是返回false并执行else语句。
所以,就像这样潜入一个变量。
蓝宝石连接74:45:CE:97:90:72 x=$?回波$x
https://askubuntu.com/questions/1388805
复制相似问题