Shell script not effective from switch. Why?

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
bxlouis
Posts: 45
Joined: Wednesday 16 December 2015 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Belgium
Contact:

Shell script not effective from switch. Why?

Post by bxlouis »

Hi y'all!

I am trying to make a switch wake-up and sleep my Mac.

In order to make it sleep, I have done the following:

1. Install "expect" package on my RPi.
2. Created a script test.sh in domoticz/scripts/ (cf. below)
3. Ran chmod +x test.sh
4. Created a virtual switch and mentioned "script://test.sh" in the "Off action" field.

When turning the switch off, I see in the log that the script was triggered.

Code: Select all

Status: Executing script: /home/pi/domoticz/scripts/test.sh
But nothing happens and my mac does not go to sleep.
However, if I ssh to my RPi and manually execute the script, my Mac goes to sleep instantly.
I can't figure out why the script does not work when called from the Domoticz switch.

Can someone help me understand and maybe fix my issue ?


test.sh:

Code: Select all

#!/usr/bin/expect -f
spawn ssh MAC_USER@MAC_IP "pmset sleepnow"
expect "assword:"
send "MAC_USER_PASSWORD\r"
interact
Last edited by bxlouis on Saturday 25 April 2020 9:44, edited 1 time in total.
User avatar
FireWizard
Posts: 1905
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Shell script not effective from switch. Why?

Post by FireWizard »

Hi,

@bxlouis

First of all, I think there is a typo in your script:
expect "assword:"
.
Shouldn't that be password?

Very recently there was another issue on this forum, discussing a similar problem.

I suggest you read that first and if possible make some tests.

See: https://www.domoticz.com/forum/viewtopi ... 6&start=20

This was a "brain breaker", but finally also a solution.

Regards
bxlouis
Posts: 45
Joined: Wednesday 16 December 2015 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Belgium
Contact:

Re: Shell script not effective from switch. Why?

Post by bxlouis »

Hi @FireWizard,

Thank you for your reply. I will take the time to read the thread carefully as after my first reading, I don't quite understand how they solved the issue!

No typo in the script, it works fine like that. I guess the author of the script removed the "p" that to avoid any mistake related to the shell displaying "password:" or "Password:".
bxlouis
Posts: 45
Joined: Wednesday 16 December 2015 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Belgium
Contact:

Re: Shell script not effective from switch. Why?

Post by bxlouis »

After searching for a while I decided to go for another solution which works well.
The issue is not solved but I got what I wanted to achieve.

If anyone is interested, this allows to setup a switch which will wake up and put your Mac to sleep.
It is rather quick and dirty and I am sure there are better ways to script this, but I was not able to find an efficient way to do this on the Domoticz forum.
Hope it helps anyone who has been trying to wake and put his mac to sleep out there.

1. ssh to your Raspberry pi
2. Run "sudo apt-get install sshpass"
3. In Domoticz, create a dummy switch called "iMac" (or whatever you want)
4. Create a Lua script with Trigger set to "Device"
5. Paste the script below and edit the values to match your config

Code: Select all

commandArray = {}

if (devicechanged['iMac'] == "On") then
    print("Waking up iMac...") 
    os.execute('/usr/sbin/etherwake YOURMACMACADDRESS')
    os.execute("sshpass -p 'YOURMACUSERPASSWORD' ssh -o StrictHostKeyChecking=no YOURMACUSER@YOURMACIP caffeinate -u -t 2")
elseif (devicechanged['iMac'] == "Off") then
    print("Putting iMac to sleep...")
    os.execute("sshpass -p 'YOURMACUSERPASSWORD' ssh -o StrictHostKeyChecking=no YOURMACUSER@YOURMACIP pmset sleepnow")
end

return commandArray
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Shell script not effective from switch. Why?

Post by gizmocuz »

To wake-up a computer you could also use the build in WOL (Wakeup on Lan) hardware type
This does not let it sleep... But as i think you are going to work on your mac after waking it up, you could issue a sleep command,
or just configure it to sleep after 30 minutes on no activity ?
Quality outlives Quantity!
User avatar
FireWizard
Posts: 1905
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Shell script not effective from switch. Why?

Post by FireWizard »

Hi,

@bxlouis

If you are satisfied with your solution, I leave it from my side.

Regards
bxlouis
Posts: 45
Joined: Wednesday 16 December 2015 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Belgium
Contact:

Re: Shell script not effective from switch. Why?

Post by bxlouis »

Hi @Gizmocuz,

I have been using the WOL hardware for the last few years. However I have never found a right solution to put a Mac to sleep immediately from a switch on the forum.

At the beginning (cf. first message) I tried to simply add the sleep command to the OFF action but I ran into the issue I described.
I thought it was because I was using the switch provided by the WOL hardware so I tried with a virtual switch but got the same issue so I opened this thread.

I still don’t understand why my ssh script is not working with the ON/OFF actions from the switch while it works perfectly when executed from the terminal. I tried several things, change ownership to root and back to pi, moving the script to /home/pi/ instead of ~/domoticz/scripts/, use relative and absolute path, set different rights to the script but without any success unfortunately.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest