首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cron和bash脚本显示不同的日志。

Cron和bash脚本显示不同的日志。
EN

Ask Ubuntu用户
提问于 2022-01-23 16:49:37
回答 1查看 127关注 0票数 0

我有一个简单的脚本来寻找一个配对的蓝牙耳机,如下所示,并安排它在cron中运行每分钟。它工作并连接到配对的蓝牙耳机,当它被打开时,如果蓝牙设备按预期关闭,它就会失败。为了调试cron调度程序,我在日志中捕获了脚本的日志。该日志只添加0标准输出,而不添加1 stderr。

脚本

代码语言:javascript
复制
#!/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:

代码语言:javascript
复制
#To Connect Bluetooth automatically
* * * * * /home/xxxxx/Documents/Shell/scripts/bluetooth.sh >> /home/xxxxx/Documents/Shell/scripts/logs/bluetooth.log 2>&1

当我手动运行脚本时,它会根据连接捕获stderr和stdout。

代码语言:javascript
复制
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的输出总是低于成功连接的输出。

代码语言:javascript
复制
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的日志就没有错误吗?

EN

回答 1

Ask Ubuntu用户

发布于 2022-01-23 17:56:08

首先,即使您的手动运行也没有给出预期的结果。该设备未能连接,最终的输出应该是“没有连接到索尼耳机”。

选项1:问题在于"echo $?“。删除这个语句,你就会没事的。

选项2:回显$?总是计算为0,因此如果总是返回false并执行else语句。

所以,就像这样潜入一个变量。

蓝宝石连接74:45:CE:97:90:72 x=$?回波$x

票数 -1
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1388805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档