Exec binding script does not work when turning off [Solved]

  • Platform information:
    • Hardware: Raspberry Pi 3
    • OS: Raspbian GNU/Linux 9
    • Java Runtime Environment: openjdk version “1.8.0_152”
    • openHAB version: 2.4.0 Release Build
  • Issue of the topic:
    I created a script to wake my PC using Wake on LAN and suspend it (hibernate).
    When I execute the script from the command line everything works fine, passing “on” as a parameter turns the PC on, passing “off” as a parameter turns the PC off, and passing “status” as a parameter correctly displays the status of the PC (off for a while, while booting up, on when done)

However when I create a Thing with the exec binding only the “on” and “status” part work. I directed some log messages to a file in the “off” section and they are appended to the file correctly, but the hibernate command does not seem to come through.

here is the script:
#!/bin/bash

MAC_ADDRESS="XX:XX:XX:XX:XX:XX"
IP_ADDRESS="XXX.XXX.XXX.XXX"
USERNAME='user'
PASSWORD='password'

case "${1}" in
        [oO][nN])
                exec sudo etherwake $MAC_ADDRESS &> /dev/null
                ;;

        [oO][fF][fF])
                date >> /var/log/pascal.log
                echo "PC off " >> /var/log/pascal.log
                whoami >> /var/log/pascal.log
                echo "timeout 3 sshpass -p $PASSWORD ssh $USERNAME@$IP_ADDRESS shutdown /h " >> /var/log/pascal.log
                echo "$PATH" >> /var/log/pascal.log
                echo "" >> /var/log/pascal.log

                exec timeout 3 sshpass -p $PASSWORD ssh $USERNAME@$IP_ADDRESS shutdown /h &>/dev/null
                ;;

        [sS][tT][aA][tT][uU][sS])
                timeout 1 ping -c 1 -n $IP_ADDRESS &>/dev/null
                if [ $? -eq 0 ]; then
                        echo "ON"
                        exit 0
                else
                        echo "OFF"
                        exit 1
                fi
                ;;

        *)
                echo
                echo "Usage: pascals_pc <on|off|status>"
                echo
                exit 1
                ;;
esac

exit 0

I placed it in /usr/local/bin/ and added ALL ALL=NOPASSWD: /usr/sbin/etherwake to my sudoers file using visudo

  • Please post configurations (if applicable):

    • Things configuration related to the issue

      Thing exec:command:pascals_pc "Pascals PC Control" [ command="/usr/local/bin/pascals_pc %2$s", interval=0, timeout=10, autorun=true ]
      Thing exec:command:pascals_pc_status "Pascals PC Status" [ command="/usr/local/bin/pascals_pc status", interval=20, timeout=5 ]
      
    • Items configuration related to the issue

      String network_device_pascals_pc_control "Pascals PC Control" { channel="exec:command:pascals_pc:input", channel="exec:command:pascals_pc_status:output", autoupdate="false" }
      String network_device_pascals_pc_status "Pascals PC Status" { channel="exec:command:pascals_pc_status:output" }
      Switch network_device_pascals_pc_switch "Pascals PC" ["Switchable"]
      
    • Sitemap configuration related to the issue

      sitemap test label="My home automation" {
          Frame label="Test Frame" {
              Switch item=network_device_pascals_pc_switch
          }
      }
      
    • Rules code related to the issue

      rule "Pascals PC Switch"
      when
              Item network_device_pascals_pc_switch changed
      then
              logInfo("Network", "Switching Pascals PC " + network_device_pascals_pc_switch.state.toString + ": " + network_device_pascals_pc_control.state.toString)
              network_device_pascals_pc_status.postUpdate(network_device_pascals_pc_switch.state.toString)
              network_device_pascals_pc_control.sendCommand(network_device_pascals_pc_switch.state.toString)
      end
      
      rule "Pascals PC State"
      when
              Item network_device_pascals_pc_status changed
      then
              logInfo("Network", "Switching Pascals PC " + network_device_pascals_pc_status.state.toString + ": " + network_device_pascals_pc_control.state.toString)
              network_device_pascals_pc_switch.postUpdate(network_device_pascals_pc_status.state.toString)
      end
      
  • If logs where generated please post these here using code fences:
    None besides the output from the rule

Well in an effort to not look stupid I tried to fix the problem myself one last time. As it turns out I am stupid and someone created a post for stupid people like me: How to solve Exec binding problems .

My openhab user did not have my PC in its known host file. So I copied the file from the openhabian user.