Score:0

Cron and bash script shows different logs

de flag

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?

bac0n avatar
cn flag
Think you should move your script to systemd instead.
Zanna avatar
kr flag
Doubt it will make a functional difference, but FYI `if` checks the exit status of a command. That is how it works. One need never write things like `some command; if [ $? = 0 ]; then ...` Instead: `if some command; then ...`
Moriartyalex avatar
de flag
@Zanna I used ```if ! bluetoothctl connect 74:45:CE:97:90:72 #if [ $? == 1 ]``` still it works the same. The manaul execution of the script prints the if statement if not able to connect to device. But the cron execution and it's log show only else.
Zanna avatar
kr flag
well, I can never get `cron` to work, so can't comment on that!
Score:-1
in flag

Firstly, even your manual run did not give expected results. The device failed to connect and the final output shoud have been "Failed to connce to sony headset".

Option 1: The issue is with "echo $?". Remove this statement and you will be fine.

Option 2: echo $? is always evaluating to 0 due to which if [$? == 1 ] always returns false and executes the else statement.

So, just sneak in a variable like this.

bluetoothctl connect 74:45:CE:97:90:72 x=$? echo $x if [ $x == 1 ]

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.