How to restart services with python ?

Python and python framework

Moderator: leecollings

Post Reply
tontze
Posts: 317
Joined: Thursday 12 January 2017 15:30
Target OS: Linux
Domoticz version: Beta Ch
Location: Finland
Contact:

How to restart services with python ?

Post by tontze »

Hi !

I have a conflict problem with my bt beacon and miflora sensor. Whenever i run miflora update, beacon stops responding, so i thought to restart
beacon service in the end of a miflora python script. How is it easiest to do ?
-----------------------------------------
Smartthings
zigbee2mqtt
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
esp8266MiLight Hub
OpenHab/HomeAssistant/Domoticz
HP T610 & Debian 5.10.19-1 x86_64[/b]
ubee
Posts: 66
Joined: Tuesday 10 February 2015 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Sweden
Contact:

Re: How to restart services with python ?

Post by ubee »

I would have started the beacon service as a systemd service. Then it will be automatically restarted by the Linux system if it terminates for some reason. If you do not know how to start a script as a systemd service, you can find an example on github, bengtner/GPIOinput. You write a small service definition file and put in /etc/systemd/system, then use the systemctl command to enable the service.
tontze
Posts: 317
Joined: Thursday 12 January 2017 15:30
Target OS: Linux
Domoticz version: Beta Ch
Location: Finland
Contact:

Re: How to restart services with python ?

Post by tontze »

Beaconing is made by this wiki :

http://www.domoticz.com/wiki/Presence_d ... gy_Beacon)

And is it run from init.d

It runs hci0 port, but if anything else accesses it, it will stop responding, im not sure does it die, or just doesnt respond anymore ..

Im noob on this so .. :)

EDIT: by checking status of a service, i see it is running, but not responding

-T
-----------------------------------------
Smartthings
zigbee2mqtt
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
esp8266MiLight Hub
OpenHab/HomeAssistant/Domoticz
HP T610 & Debian 5.10.19-1 x86_64[/b]
ubee
Posts: 66
Joined: Tuesday 10 February 2015 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Sweden
Contact:

Re: How to restart services with python ?

Post by ubee »

I see, since status reports "running" no system surveillance will solve your problem.

I'm sorry, but I can't help you in this matter.
tontze
Posts: 317
Joined: Thursday 12 January 2017 15:30
Target OS: Linux
Domoticz version: Beta Ch
Location: Finland
Contact:

Re: How to restart services with python ?

Post by tontze »

ubee wrote:I see, since status reports "running" no system surveillance will solve your problem.

I'm sorry, but I can't help you in this matter.
Ok, thnx anyways, an attempt was made :D

Does anyone else have ideas ? I simply would like to "sudo /etc/init.d/beqacon restart" service at the end of miflora script. Mi flora is run every 6hours.

I just dont know how to do it with python :/
-----------------------------------------
Smartthings
zigbee2mqtt
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
esp8266MiLight Hub
OpenHab/HomeAssistant/Domoticz
HP T610 & Debian 5.10.19-1 x86_64[/b]
gerardvs
Posts: 81
Joined: Sunday 04 January 2015 0:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest-1
Location: /dev/null
Contact:

Re: How to restart services with python ?

Post by gerardvs »

Something like this?

Code: Select all

import os
os.system("sudo /etc/init.d/beqacon restart")
Make sure sudo is not asking for a password.

--Gerard
tontze
Posts: 317
Joined: Thursday 12 January 2017 15:30
Target OS: Linux
Domoticz version: Beta Ch
Location: Finland
Contact:

Re: How to restart services with python ?

Post by tontze »

gerardvs wrote:Something like this?

Code: Select all

import os
os.system("sudo /etc/init.d/beqacon restart")
Make sure sudo is not asking for a password.

--Gerard
Thanks, that did it :)
-----------------------------------------
Smartthings
zigbee2mqtt
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
esp8266MiLight Hub
OpenHab/HomeAssistant/Domoticz
HP T610 & Debian 5.10.19-1 x86_64[/b]
Lora46
Posts: 1
Joined: Wednesday 21 June 2017 7:10
Target OS: Windows
Domoticz version:
Contact:

Re: How to restart services with python ?

Post by Lora46 »

Ease from there you'll have the ability to download Fildo the within the Fildo App. The Fildo Apk could be downloaded.
devros
Posts: 183
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: How to restart services with python ?

Post by devros »

hello, iv had same problem as you, so i changed script
with this command

Code: Select all

os.system("timeout 2 hcitool lescan 5")
miflora works OK in my tests and it works without sudo

Code: Select all

#!/usr/bin/python3
import urllib.request
import base64
import time
import os
from miflora.miflora_poller import MiFloraPoller, \
    MI_CONDUCTIVITY, MI_MOISTURE, MI_LIGHT, MI_TEMPERATURE, MI_BATTERY

# Settings for the domoticz server

# Forum see: http://domoticz.com/forum/viewtopic.php?f=56&t=13306&hilit=mi+flora&start=20#p105255

domoticzserver   = "XXXX"
domoticzusername = "XXXX"
domoticzpassword = "XXXX"

# So id devices use: sudo hcitool lescan
base64string = base64.encodestring(('%s:%s' % (domoticzusername, domoticzpassword)).encode()).decode().replace('\n', '')

def domoticzrequest (url):
  request = urllib.request.Request(url)
  request.add_header("Authorization", "Basic %s" % base64string)
  response = urllib.request.urlopen(request)
  return response.read()

def update(address,idx_moist,idx_temp,idx_lux,idx_cond):

    poller = MiFloraPoller(address)
    loop = 0
    try:
        temp = poller.parameter_value("temperature")
    except:
        temp = 201

    while loop < 2 and temp > 200:
        print("Patched: Error reading value retry after 5 seconds...\n")
        time.sleep(5)
        poller = MiFloraPoller(address)
        loop += 1
        try:
            temp = poller.parameter_value("temperature")
        except:
            temp = 201

    if temp > 200:
        print("Patched: Error reading value\n")
        return

    global domoticzserver

    print("Mi Flora: " + address)
    print("Firmware: {}".format(poller.firmware_version()))
    print("Name: {}".format(poller.name()))
    print("Temperature: {}°C".format(poller.parameter_value("temperature")))
    print("Moisture: {}%".format(poller.parameter_value(MI_MOISTURE)))
    print("Light: {} lux".format(poller.parameter_value(MI_LIGHT)))
    print("Fertility: {} uS/cm?".format(poller.parameter_value(MI_CONDUCTIVITY)))
    print("Battery: {}%".format(poller.parameter_value(MI_BATTERY)))

    val_bat  = "{}".format(poller.parameter_value(MI_BATTERY))

    # Update temp
    val_temp = "{}".format(poller.parameter_value("temperature"))
    domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + idx_temp + "&nvalue=0&svalue=" + val_temp + "&battery=" + val_bat)

    # Update lux
    val_lux = "{}".format(poller.parameter_value(MI_LIGHT))
    domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + idx_lux + "&svalue=" + val_lux + "&battery=" + val_bat)

    # Update moisture
    val_moist = "{}".format(poller.parameter_value(MI_MOISTURE))
    domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + idx_moist + "&svalue=" + val_moist + "&battery=" + val_bat)

    # Update fertility
    val_cond = "{}".format(poller.parameter_value(MI_CONDUCTIVITY))
    domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + idx_cond + "&svalue=" + val_cond + "&battery=" + val_bat)
    time.sleep(1)

# format address, moist (%), temp (°C), lux, fertility
os.system("timeout 2 hcitool lescan 5")
print ("predping")
update("C4:7C:8D:61:B4:DA","142","133","136","143")
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest