1

I searched for hours already, but can't find anything regarding this:

I have a bash script that tunnels via ssh from my PC to a Pi to test certain files and if certain conditions are met. I recently added a function which tests the BLE functionality of the Pi; for this I again tunnel from the first Pi to a second one and start a script on the second one that sends a BLE advertisement. Now I check if the first Pi recognizes the advertisement. The function looks like this:

function BLE_scanning {
echo "Connecting to RPI BLE advertiser..."
#starting RPI BLE advertisement
if ssh -q root@$RPIP exit
then
    func_return $? && echo "Connected to RPI"
ssh root@$RPIP './bluez-5.43/test/example-advertisement' &
PID=$(ps -ef|pgrep -f "ssh root@$RBIP")
else
    func_return $? && echo "Can't connect to RPI. Check the device. Tests will fail."
fi
#searching for RPI BLE advertisement
echo "Searching BLE advertisement..."
if timeout 30s grep -m 1 "entry $RPMAC" <(tail -f $LOG)
then
    func_return $? && echo "Advertisement found from right MAC $RPMAC"
    sleep 1
    #added this to kill the ssh connection to the 2nd Pi, which didn't change anything
    kill -15 $PID
    #killing the advertisement on the 2nd Pi, which works perfectly
    ssh root@$RPIP 'killall -15 example-advertisement'
else
    func_return $? && echo "No advertisement found"
    sleep 1
    kill -15 $PID
    ssh root@$RPIP 'killall -15 example-advertisement'
fi
}

This works fine, it starts the advertisement script on the second Pi, the first correctly detects the MAC and closes the "example-advertisement". The real problem: I want the function only executed under certain conditions.

REBOOT="path/to/a/created/flagfile"
if [ ! -f $REBOOT ]
then
    BLE_scanning
    echo "This gets correctly echoed after the function executed."
    echo "I can add as many commands after the function as I want."
fi
echo "But THIS echo just doesn't get executed, no matter what." 
exit 0

The script just exits BEFORE the "echo" after the "fi". I'm 100% sure it's no syntax error with if and fi or something. It just stops at the last command of the if-commands. I also normally run many, many more functions which all get executed and the "if" properly exits after. The BLE funtion however is the only one starting a ssh connection.

I also used "jobs -l" which shows the PID of the ssh connection to the second Pi and says "exit", so it stopped and should be fine. Any ideas? Is it because I tunnel from an already open tunnel? Something something sub shell?

Btw. the function "func_return" just takes the exit code of the last executed command and puts out "OK" for 0 and "FAILED" for 1, it does nothing really.

Jojo
  • 13
  • 3
  • 1
    Try trapping SIGTERM. My guess is that PID is not what you expect because using `pgrep` to find the pid is clumsy, and you're sending a signal to the main script. Rather than trying to parse the output of `pgrep`, just use the value in `$!` – William Pursell Jul 20 '18 at 14:34
  • @WilliamPursell Good advice, but killing the main script would kill it before `BLE_scanning` completes, and the OP claims the rest of the `echo` statements after that call execute. – chepner Jul 20 '18 at 14:39
  • You'll need to produce a minimal example that *we* can run to reproduce the problem; I don't see anything in your code that would produce the behavior you describe. – chepner Jul 20 '18 at 14:40
  • Tried it and it's exactly the same output and outcome. I just now tried to run the function unconditional and echo something after it. Doesn't echo, it just exits to my start script. I'm really confused. Why would it execute everything in the if-condition, which is clearly after the function (or is it?), but nothing further? Will try to rewrite it so you guys can run it. Of course you'll need to connect via ssh somewhere – Jojo Jul 20 '18 at 14:56

0 Answers0