action to reboot / shutdown server

User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

action to reboot / shutdown server

Post by kw123 »

put this into an action / server / execute script :

Code: Select all

import os
myPassword = "put my password here"

# for reboot:
cmd = "/sbin/reboot"

# for shutdown
#cmd = "/sbin/shutdown now"

os.system("echo '"+ myPassword + "' | sudo -S "+cmd+" &")
when executed the mac indigo server will reboot or will shut down depending on the option chosen.

Karl
Attachments
Screen Shot 2018-11-25 at 19.14.02.png
Screen Shot 2018-11-25 at 19.14.02.png (45.37 KiB) Viewed 12593 times
howartp
Posts: 4559
Joined: Thu Jan 09, 2014 4:43 pm
Location: West Yorkshire, UK

Re: action to reboot / shutdown server

Post by howartp »

Awesome!


Sent from my iPhone using Tapatalk Pro
User avatar
Colorado4Wheeler
Posts: 2794
Joined: Mon Jul 20, 2009 10:48 am
Location: Colorado

Re: action to reboot / shutdown server

Post by Colorado4Wheeler »

Mac Commander also does this as well as lets you run anything on a remote Mac.
My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

Re: action to reboot / shutdown server

Post by kw123 »

@Colorado4Wheeler.. downloaded your plugin.. how would you use it to reboot / shutdown the server. I do see sleep, stop/start apps.
one thing I really would like to to do is to stop a plugin wait and then restart it, not the reload action of indigo.
I did a "tell system ... move , pick .." but that depends on plugin position in the menu ..
Indigo does not expose a lot of properties to apple script (and will likely not add any) so that a direct command is not available.

thanks

Karl
ps looking at your plugin code inspires me to try a lot of new things.
Attachments
Screen Shot 2018-11-27 at 10.49.22.png
Screen Shot 2018-11-27 at 10.49.22.png (21.03 KiB) Viewed 12471 times
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

action to reboot / shutdown server

Post by kw123 »

To make indigo self recover after power failures, hanging of server ..
3 tools:
  1. wake on lan
    on an raspberry pi:
    wakeonlan mac#OfIndigoEthernetCard
    - pibeacon can be set to do that: edit pi-server and add the mac# in the wake on .. field
    you need to enable wake on lan on the Mac in: System prefs/energy saver/ "wake for network access"
    this only works when the mac is asleep not when it is shutdown
  2. switchbot bot attached to the MAC, that switch can be remotely operated (pibeacon) to shutdown or restart the power on the mac (viewtopic.php?f=187&t=25391&p=204194#p204194
  3. run a script on the indigo server that pings the nearest switch, if no success reboot, see below

Code: Select all

# karl wachs 2021-07-14, use as you see fit
# put this script into the action server script  of a schedule, run it once per minute
# this script should run every minute. It will check if at least ONE ping to x different ip numbers works
# if ok exit, set lastNetworkOKTest to current time stamp
# if not ok check if last ok test is older than 3 minutes ( give eg switches time to reboot during upgrades etc )
#	if lastOKtest < 3  minutes ago: exit
#	if lastreboot < 15 minutes ago: exit
#	reboot 
# you need to set password  below
# make sure that execution time stays below 10 secs (max # of ip numbers to test = 3)
#   ==> max time is 2 rounds of pings for each ip = 2*3 secs + 2 secs wait = 8 secs 
#  it will create all necessary variables and folder
# all the variables usedshow time since epoch not at datestring
#
import time
import subprocess

testMaxEvery			= 1.*59
rebootIfOkTestOlderThan	= 3.*60 
rebootNotMoreOftenThan	= 20.*60 
pwd						= "your mac password here "
ipnumbersForPingTest	= ["192.168.1.10","192.168.1.1","192.168.1.2"]# use 1..3 ip numbers
numberOfPings			= 2
logOK					= True
logFailed				= True

try:	  indigo.variables.folder.create(u"rebootIfNoNetwork")
except: pass

try:	  indigo.variable.create(u"lastNetworkTest", "0","rebootIfNoNetwork")
except:  pass
try:	  lastNetworkTest = float(indigo.variables["lastNetworkTest"].value)
except: lastNetworkTest = 0

try:	  indigo.variable.create(u"lastOKNetworkTest", "0","rebootIfNoNetwork")
except: pass
try:	  lastNetworkOKTest = float(indigo.variables["lastNetworkOKTest"].value)
except: lastNetworkOKTest = 0

try:	  indigo.variable.create(u"lastReboot", "0","rebootIfNoNetwork")
except: pass
try:	  lastReboot = float(indigo.variables["lastReboot"].value)
except: lastReboot = 0

now = time.time()
if now - lastNetworkTest < testMaxEvery: exit()
indigo.variable.updateValue(u"lastNetworkTest", str(now))

# send  ping tries
for ipN in ipnumbersForPingTest:
	pingCmd= "/sbin/ping -c {} -W 1 {}".format(numberOfPings , ipN)
	ret = subprocess.Popen(pingCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
	if logOK: indigo.server.log(">>reboot if no network<< testing: {}".format(pingCmd))
	if ret.find("100.0% packet loss") == -1:  # accept at least one ping ok
		if logOK: indigo.server.log(">>reboot if no network<<: test passed ")
		indigo.variable.updateValue(u"lastOKNetworkTest", str(now))
		exit()

if now - lastNetworkOKTest < rebootIfOkTestOlderThan: 
	if logFailed: indigo.server.log(">>reboot if no network<< test failed, but delaying reboot for {:.0f} secs, waiting for next check".format(rebootIfOkTestOlderThan-(now - lastNetworkOKTest)))
	exit()

if now - lastReboot < rebootNotMoreOftenThan: 
	if logFailed: indigo.server.log(">>reboot if no network<< test failed, but delaying reboot for {:.0f} secs, last reboot was done previuosly".format(rebootNotMoreOftenThan-(now - lastReboot)))
	exit()

indigo.variable.updateValue(u"lastReboot", str(now))

if logFailed: indigo.server.log(">>reboot if no network<< failed, rebooting in 2 secs")
time.sleep(2)
cmd = u"echo '"+ pwd + u"' | sudo -S /sbin/reboot now"
subprocess.Popen(cmd, shell=True)
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

action to reboot / shutdown server

Post by kw123 »

and one more option, if you loose power and power back in is not to long into the future, you can set the mac to power on 3 hours from now, run it every 10 minutes.
So if the power to your mac comes back with the next 3 hours, it will power on, if it is later, it will wait 3+24 hours, better than nothing.

Code: Select all

# karl wachs 2021-07-14, use as you see fit
# put this script into the action server script  of a schedule, run it once per minute
#  it will then use pmset to set "poweron "to current time + 3 hours, it is only active when MAC is off, when MAC runs nothing happens
import datetime
import subprocess
pwd = "your mac password"

newBootHour =  datetime.datetime.now() + datetime.timedelta(hours = 3)
boottime = "{:02d}:{:02d}:00".format(newBootHour.hour,newBootHour.minute) 
indigo.server.log(">>setting boot time << {}".format(boottime) )

cmd = u"/bin/echo '"+ pwd + "' | /usr/bin/sudo -S /usr/bin/pmset repeat wakeorpoweron MTWRFSU " +boottime

#indigo.server.log(">>setting boot time << {}".format(cmd) )
ret = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
u
Bildhauer
Posts: 160
Joined: Mon Sep 30, 2019 2:10 pm
Location: 22113 Oststeinbek (near Hamburg), Germany

Re: action to reboot / shutdown server

Post by Bildhauer »

I'm not sure if this is the correct context but I try.

I have an Automator app which quits and and 10 seconds will start the indigo server again. But how can I call/ start this app?

Thanks in advance for your help!
User avatar
Different Computers
Posts: 2730
Joined: Sat Jan 02, 2016 10:07 am
Location: East Coast
Contact:

Re: action to reboot / shutdown server

Post by Different Computers »

Closest thing I can think of is to have the Quit automation do a periodic test for a response from Indigo, and to NOT proceed if Indigo responds.
Sonoma on a Mac Mini M1 running Airfoil Pro, Bond Home, Camect, Roku Network Remote, Hue Lights, DomoPad, Adapters, Home Assistant Agent, HomeKitLinkSiri, EPS Smart Dimmer, Fantastic Weather, Nanoleaf, LED Simple Effects, Grafana, Yamaha RX Receiver.
Bildhauer
Posts: 160
Joined: Mon Sep 30, 2019 2:10 pm
Location: 22113 Oststeinbek (near Hamburg), Germany

Re: action to reboot / shutdown server

Post by Bildhauer »

If the last entry is dedicated ,my post:

The only thing I'm looking for is a way to start this external App which is located on my desktop.

I think it's very ieasy but I cannot find a way to start it.
Bildhauer
Posts: 160
Joined: Mon Sep 30, 2019 2:10 pm
Location: 22113 Oststeinbek (near Hamburg), Germany

Re: action to reboot / shutdown server

Post by Bildhauer »

Just found a solution to my issue:

Code: Select all

import os
path = "/Applications/Safari.app"
os.system(f"open {path}")
Post Reply

Return to “Karl's Plugins and Scripts”