pihome-shc / pihome

PiHome - Smart Heating, Ventilation and Air Conditioning (HVAC)
http://www.pihome.eu
Other
52 stars 25 forks source link

Cronjob management #327

Closed dvdcut closed 3 years ago

dvdcut commented 3 years ago

while working on #325 it got me thinking about cronjobs, can we manage these jobs some other way. i mean one script manage all cronjobs and it runs once every 60seconds this way we'll have only one entry in crobtab? we can control this from web interface what to run and how frequent. This is just very vague question so any reason we shouldn't do this? or any suggestion to do this?

pihome-shc commented 3 years ago

@dvdcut i looked at this in the past but due to time constrains i never had chance to revisit this. there are some project on github written in php.

dvdcut commented 3 years ago

i think all cron jobs can be managed from one php script and all jobs with schedule can be saved in database, small modification to exiting crontab table, i will try to find something on github so i don't have re-invent the wheel 😄

CREATE TABLE `crontab` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `status` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
    `min` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
    `hour` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
    `day` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
    `month` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
    `weekday` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
    `command` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
    `output` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
    `last_run` DATETIME NULL DEFAULT NULL,
    `comments` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
    PRIMARY KEY (`id`) USING BTREE
)
COMMENT='PiHome Smart Heating - Manage Crontab from web interface '
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2
;
pihome-shc commented 3 years ago

feel free to work on this, i'll try to give you my input if needed.

sandreialexandru commented 3 years ago
# Schedule Library imported 
import schedule 
import time 
import os

# Functions setup 
def system_c(): 
    system = os.popen("usr/bin/php /var/www/cron/system_c.php \n") 
    system_output = system.read()
    print(system_output)

def system_c_exec():
    return system_c()

def weather_update(): 
    weather = os.popen("/usr/bin/php /var/www/cron/weather_update.php \n")
    weather_output = weather.read()
    print(weather_output) 

def weather_update_exec():
    return weather_update()

def reboot_wifi(): 
    rwifi = os.popen("sh /var/www/cron/reboot_wifi.sh \n") 
    rwifi_output = rwifi.read()
    print(rwifi_output)

def reboot_wifi_exec():
    return reboot_wifi()

def check_gw(): 
    chk_gw = os.popen("php /var/www/cron/check_gw.php \n")
    chk_gw_output = chk_gw.read()
    print(chk_gw_output)

def check_gw_exec():
    return check_gw()

def boiler(): 
    stream = os.popen("/usr/bin/php /var/www/cron/boiler.php \n")
    boiler_output = stream.read()
    print (boiler_output)

def boiler_exec():
    return boiler()

def selfservice(): 
    self = os.popen("/usr/bin/php /var/www/cron/selfservice.php \n")
    self_output = self.read()
    print (self_output)

def selfservice_exec():
    return selfservice()

# Task scheduling 

schedule.every(10).minutes.do(system_c)
schedule.every(30).minutes.do(weather_update) 
schedule.every(2).minutes.do(reboot_wifi) 
schedule.every(2).minutes.do(check_gw) 
schedule.every(1).minutes.do(boiler) 
#schedule.every(3).seconds.do(selfservice) 

# Loop so that the scheduling task 
# keeps on running all time. 

while True: 

    # Checks whether a scheduled task  
    # is pending to run or not 

    schedule.run_pending() 
    time.sleep(1) 

This is my approach to a python script that does a better job at scheduling than cron. Having a GUI for management requires values (time, job command, command output...) to be stored into variables that get updated from the database. The python script can then be run at startup with rc.local What do you think?

sandreialexandru commented 3 years ago

Update:

CREATE TABLE `jobs` (
  `id` int(11) NOT NULL,
  `name` char(50) NOT NULL,
  `script` char(100) NOT NULL,
  `time` int(11) NOT NULL,
  `output` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `jobs`
--

INSERT INTO `jobs` (`id`, `name`, `script`, `time`, `output`) VALUES
(1, 'boiler', 'php /var/www/cron.boiler.php', 5, '\n   _____    _   _    _                             \n  |  __ \\  (_) | |  | |                            \n  | |__) |  _  | |__| |   ___    _ __ ___     ___  \n  |  ___/  | | |  __  |  / _ \\  | |_  \\_ \\   / _ \\ \n  | |      | | | |  | | | (_) | | | | | | | |  __/ \n  |_|      |_| |_|  |_|  \\___/  |_| |_| |_|  \\___| \n  \n      S M A R T   H E A T I N G   C O N T R O L  \n**********************************************************\n*   Boiler-On Script Version 0.53 Build Date 31/01/2018  *\n*   Update on 23/08/2020                                 *\n*                                Have Fun - PiHome.eu    *\n**********************************************************\n  \n2020-10-15 18:59:27 - Boiler Script Started \n2020-10-15 18:59:27 - Day of the Week: 4 \n---------------------------------------------------------------------------------------- \n2020-10-15 18:59:27 - Zone: Sensor Reading     21.60 \n2020-10-15 18:59:27 - Zone: Weather Factor     0.6 \n2020-10-15 18:59:27 - Zone: DeadBand           0.5 \n2020-10-15 18:59:27 - Zone: Cut In Temperature        0 \n2020-10-15 18:59:27 - Zone: Cut Out Temperature       0 \n2020-10-15 18:59:27 - Zone: Mode       0 \n2020-10-15 18:59:27 - Zone ID: 38 \n2020-10-15 18:59:27 - Zone: Ground Floor Controller: 0 Controller Child: 23 Zone Status: 0 \n2020-10-15 18:59:27 - Zone: Ground Floor Stop Cause: No Schedule - Target C:0 Zone C:21.60 \n---------------------------------------------------------------------------------------- \n2020-10-15 18:59:27 - Zone: GIOP Relay Status: 1 (0=On, 1=Off) \n2020-10-15 18:59:27 - Boiler Switched Off At: 2020-10-15 15:15:24\n2020-10-15 18:59:28 - Boiler GIOP: 12 Status: 1 (0=On, 1=Off) \n2020-10-15 18:59:28 - Boiler Fired Status: 0 \n2020-10-15 18:59:28 - Boiler Hysteresis Status: 0 \n---------------------------------------------------------------------------------------- \n2020-10-15 18:59:28 - Boiler Script Ended \n****************************************************************************************  \n\n'),
(2, 'check_gw', 'php /var/www/cron/check_gw.php \\n', 5, '\n   _____    _   _    _                             \n  |  __ \\  (_) | |  | |                            \n  | |__) |  _  | |__| |   ___    _ __ ___     ___  \n  |  ___/  | | |  __  |  / _ \\  | |_  \\_ \\   / _ \\ \n  | |      | | | |  | | | (_) | | | | | | | |  __/ \n  |_|      |_| |_|  |_|  \\___/  |_| |_| |_|  \\___| \n  \n      S M A R T   H E A T I N G   C O N T R O L  \n********************************************************\n*   Gateway Script Version 0.3 Build Date 22/01/2018   *\n*          Last Modification Date 24/04/2020           *\n*                                Have Fun - PiHome.eu  *\n********************************************************\n  \n2020-10-15 18:59:27 - Python Gateway Script Status Check Script Started \n2020-10-15 18:59:27 - Python Gateway Script for Gateway is Running \n2020-10-15 18:59:27 - The PID is: 935 \n--------------------------------------------------------------------------\n2020-10-15 18:59:27 - Python Gateway Script Status Check Script Ended \n***************************************************************************\n'),
(3, 'system_c', 'php /var/www/cron/system_c.php \\n', 5, '\n   _____    _   _    _                             \n  |  __ \\  (_) | |  | |                            \n  | |__) |  _  | |__| |   ___    _ __ ___     ___  \n  |  ___/  | | |  __  |  / _ \\  | |_  \\_ \\   / _ \\ \n  | |      | | | |  | | | (_) | | | | | | | |  __/ \n  |_|      |_| |_|  |_|  \\___/  |_| |_| |_|  \\___| \n  \n      S M A R T   H E A T I N G   C O N T R O L  \n********************************************************\n* System Temperature Version 0.4 Build Date 31/03/2018 *\n* Update on 30/03/2020                                 *\n*                                 Have Fun - PiHome.eu *\n********************************************************\n  \n2020-10-15 18:59:28 - System Temperature: 50.5\n'),
(4, 'weather_update', 'php /var/www/cron/weather_update.php \\n', 5, '\n   _____    _   _    _                             \n  |  __ \\  (_) | |  | |                            \n  | |__) |  _  | |__| |   ___    _ __ ___     ___  \n  |  ___/  | | |  __  |  / _ \\  | |_  \\_ \\   / _ \\ \n  | |      | | | |  | | | (_) | | | | | | | |  __/ \n  |_|      |_| |_|  |_|  \\___/  |_| |_| |_|  \\___| \n  \n      S M A R T   H E A T I N G   C O N T R O L  \n************************************************************\n* Weather Update Script Version 0.11 Build Date 31/01/2018 *\n* Update on 27/01/2020                                     *\n*                                     Have Fun - PiHome.eu *\n************************************************************\n  \n2020-10-15 18:59:25 - Weather Update Script Started \n2020-10-15 18:59:25 - Weather Data Downloaded \n2020-10-15 18:59:25 - Current Weather Temperature 19  \n2020-10-15 18:59:25 - Database Updated \n------------------------------------------------------ \nDate and Time:           2020-10-15 18:00:00 \nMin Temperature for day: 0 17.1  \nMax Temperature for day: 0 18.03 \nWeather:                 Clouds - broken clouds \nCloud %:                 59 \nWind Speed %:            2.02 \nHumidity :               65 \nIcon :                   04n \n------------------------------------------------------ \nDate and Time:           2020-10-15 21:00:00 \nMin Temperature for day: 1 15.94  \nMax Temperature for day: 1 16.58 \nWeather:                 Clouds - few clouds \nCloud %:                 16 \nWind Speed %:            1.34 \nHumidity :               66 \nIcon :                   02n \n------------------------------------------------------ \nDate and Time:           2020-10-16 00:00:00 \nMin Temperature for day: 2 14.79  \nMax Temperature for day: 2 15.05 \nWeather:                 Clear - clear sky \nCloud %:                 5 \nWind Speed %:            0.97 \nHumidity :               71 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-16 03:00:00 \nMin Temperature for day: 3 14.05  \nMax Temperature for day: 3 14.09 \nWeather:                 Clear - clear sky \nCloud %:                 2 \nWind Speed %:            0.96 \nHumidity :               75 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-16 06:00:00 \nMin Temperature for day: 4 15.39  \nMax Temperature for day: 4 15.39 \nWeather:                 Clouds - scattered clouds \nCloud %:                 40 \nWind Speed %:            2.42 \nHumidity :               66 \nIcon :                   03d \n------------------------------------------------------ \nDate and Time:           2020-10-16 09:00:00 \nMin Temperature for day: 5 17.56  \nMax Temperature for day: 5 17.56 \nWeather:                 Clouds - overcast clouds \nCloud %:                 96 \nWind Speed %:            2.67 \nHumidity :               62 \nIcon :                   04d \n------------------------------------------------------ \nDate and Time:           2020-10-16 12:00:00 \nMin Temperature for day: 6 19.69  \nMax Temperature for day: 6 19.69 \nWeather:                 Clouds - overcast clouds \nCloud %:                 92 \nWind Speed %:            3.09 \nHumidity :               57 \nIcon :                   04d \n------------------------------------------------------ \nDate and Time:           2020-10-16 15:00:00 \nMin Temperature for day: 7 18.25  \nMax Temperature for day: 7 18.25 \nWeather:                 Clouds - broken clouds \nCloud %:                 52 \nWind Speed %:            1.75 \nHumidity :               64 \nIcon :                   04d \n------------------------------------------------------ \nDate and Time:           2020-10-16 18:00:00 \nMin Temperature for day: 8 17.11  \nMax Temperature for day: 8 17.11 \nWeather:                 Clouds - scattered clouds \nCloud %:                 46 \nWind Speed %:            1.49 \nHumidity :               67 \nIcon :                   03n \n------------------------------------------------------ \nDate and Time:           2020-10-16 21:00:00 \nMin Temperature for day: 9 16.37  \nMax Temperature for day: 9 16.37 \nWeather:                 Rain - light rain \nCloud %:                 99 \nWind Speed %:            1.62 \nHumidity :               71 \nIcon :                   10n \n------------------------------------------------------ \nDate and Time:           2020-10-17 00:00:00 \nMin Temperature for day: 10 15.44  \nMax Temperature for day: 10 15.44 \nWeather:                 Rain - light rain \nCloud %:                 94 \nWind Speed %:            1.67 \nHumidity :               78 \nIcon :                   10n \n------------------------------------------------------ \nDate and Time:           2020-10-17 03:00:00 \nMin Temperature for day: 11 14.78  \nMax Temperature for day: 11 14.78 \nWeather:                 Rain - light rain \nCloud %:                 100 \nWind Speed %:            2.29 \nHumidity :               83 \nIcon :                   10n \n------------------------------------------------------ \nDate and Time:           2020-10-17 06:00:00 \nMin Temperature for day: 12 15.24  \nMax Temperature for day: 12 15.24 \nWeather:                 Rain - light rain \nCloud %:                 100 \nWind Speed %:            3.05 \nHumidity :               78 \nIcon :                   10d \n------------------------------------------------------ \nDate and Time:           2020-10-17 09:00:00 \nMin Temperature for day: 13 18.29  \nMax Temperature for day: 13 18.29 \nWeather:                 Clouds - scattered clouds \nCloud %:                 50 \nWind Speed %:            6.46 \nHumidity :               60 \nIcon :                   03d \n------------------------------------------------------ \nDate and Time:           2020-10-17 12:00:00 \nMin Temperature for day: 14 20.22  \nMax Temperature for day: 14 20.22 \nWeather:                 Clouds - scattered clouds \nCloud %:                 25 \nWind Speed %:            7.07 \nHumidity :               46 \nIcon :                   03d \n------------------------------------------------------ \nDate and Time:           2020-10-17 15:00:00 \nMin Temperature for day: 15 18.02  \nMax Temperature for day: 15 18.02 \nWeather:                 Clear - clear sky \nCloud %:                 0 \nWind Speed %:            6.05 \nHumidity :               46 \nIcon :                   01d \n------------------------------------------------------ \nDate and Time:           2020-10-17 18:00:00 \nMin Temperature for day: 16 14.72  \nMax Temperature for day: 16 14.72 \nWeather:                 Clear - clear sky \nCloud %:                 0 \nWind Speed %:            5.69 \nHumidity :               55 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-17 21:00:00 \nMin Temperature for day: 17 12.76  \nMax Temperature for day: 17 12.76 \nWeather:                 Clear - clear sky \nCloud %:                 0 \nWind Speed %:            4.76 \nHumidity :               60 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-18 00:00:00 \nMin Temperature for day: 18 11.38  \nMax Temperature for day: 18 11.38 \nWeather:                 Clear - clear sky \nCloud %:                 0 \nWind Speed %:            4.68 \nHumidity :               65 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-18 03:00:00 \nMin Temperature for day: 19 10.38  \nMax Temperature for day: 19 10.38 \nWeather:                 Clear - clear sky \nCloud %:                 0 \nWind Speed %:            4.29 \nHumidity :               68 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-18 06:00:00 \nMin Temperature for day: 20 10.76  \nMax Temperature for day: 20 10.76 \nWeather:                 Clear - clear sky \nCloud %:                 0 \nWind Speed %:            4.19 \nHumidity :               64 \nIcon :                   01d \n------------------------------------------------------ \nDate and Time:           2020-10-18 09:00:00 \nMin Temperature for day: 21 14.24  \nMax Temperature for day: 21 14.24 \nWeather:                 Clouds - scattered clouds \nCloud %:                 26 \nWind Speed %:            4.24 \nHumidity :               50 \nIcon :                   03d \n------------------------------------------------------ \nDate and Time:           2020-10-18 12:00:00 \nMin Temperature for day: 22 16.31  \nMax Temperature for day: 22 16.31 \nWeather:                 Clouds - broken clouds \nCloud %:                 61 \nWind Speed %:            4.45 \nHumidity :               43 \nIcon :                   04d \n------------------------------------------------------ \nDate and Time:           2020-10-18 15:00:00 \nMin Temperature for day: 23 15.24  \nMax Temperature for day: 23 15.24 \nWeather:                 Clear - clear sky \nCloud %:                 7 \nWind Speed %:            3.31 \nHumidity :               45 \nIcon :                   01d \n------------------------------------------------------ \nDate and Time:           2020-10-18 18:00:00 \nMin Temperature for day: 24 12.77  \nMax Temperature for day: 24 12.77 \nWeather:                 Clear - clear sky \nCloud %:                 3 \nWind Speed %:            3.43 \nHumidity :               50 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-18 21:00:00 \nMin Temperature for day: 25 10.95  \nMax Temperature for day: 25 10.95 \nWeather:                 Clear - clear sky \nCloud %:                 0 \nWind Speed %:            2.87 \nHumidity :               57 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-19 00:00:00 \nMin Temperature for day: 26 9.56  \nMax Temperature for day: 26 9.56 \nWeather:                 Clouds - few clouds \nCloud %:                 12 \nWind Speed %:            2.4 \nHumidity :               63 \nIcon :                   02n \n------------------------------------------------------ \nDate and Time:           2020-10-19 03:00:00 \nMin Temperature for day: 27 8.89  \nMax Temperature for day: 27 8.89 \nWeather:                 Clouds - scattered clouds \nCloud %:                 49 \nWind Speed %:            0.88 \nHumidity :               66 \nIcon :                   03n \n------------------------------------------------------ \nDate and Time:           2020-10-19 06:00:00 \nMin Temperature for day: 28 9.8  \nMax Temperature for day: 28 9.8 \nWeather:                 Clouds - scattered clouds \nCloud %:                 37 \nWind Speed %:            1.03 \nHumidity :               63 \nIcon :                   03d \n------------------------------------------------------ \nDate and Time:           2020-10-19 09:00:00 \nMin Temperature for day: 29 13.92  \nMax Temperature for day: 29 13.92 \nWeather:                 Clear - clear sky \nCloud %:                 1 \nWind Speed %:            2.72 \nHumidity :               52 \nIcon :                   01d \n------------------------------------------------------ \nDate and Time:           2020-10-19 12:00:00 \nMin Temperature for day: 30 16.07  \nMax Temperature for day: 30 16.07 \nWeather:                 Clear - clear sky \nCloud %:                 1 \nWind Speed %:            2.01 \nHumidity :               44 \nIcon :                   01d \n------------------------------------------------------ \nDate and Time:           2020-10-19 15:00:00 \nMin Temperature for day: 31 15.13  \nMax Temperature for day: 31 15.13 \nWeather:                 Clouds - scattered clouds \nCloud %:                 30 \nWind Speed %:            0.83 \nHumidity :               47 \nIcon :                   03d \n------------------------------------------------------ \nDate and Time:           2020-10-19 18:00:00 \nMin Temperature for day: 32 13.64  \nMax Temperature for day: 32 13.64 \nWeather:                 Clouds - scattered clouds \nCloud %:                 43 \nWind Speed %:            1.18 \nHumidity :               52 \nIcon :                   03n \n------------------------------------------------------ \nDate and Time:           2020-10-19 21:00:00 \nMin Temperature for day: 33 11.98  \nMax Temperature for day: 33 11.98 \nWeather:                 Clear - clear sky \nCloud %:                 1 \nWind Speed %:            1.31 \nHumidity :               59 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-20 00:00:00 \nMin Temperature for day: 34 10.75  \nMax Temperature for day: 34 10.75 \nWeather:                 Clear - clear sky \nCloud %:                 1 \nWind Speed %:            0.66 \nHumidity :               65 \nIcon :                   01n \n------------------------------------------------------ \nDate and Time:           2020-10-20 03:00:00 \nMin Temperature for day: 35 10.44  \nMax Temperature for day: 35 10.44 \nWeather:                 Clouds - scattered clouds \nCloud %:                 34 \nWind Speed %:            0.28 \nHumidity :               66 \nIcon :                   03n \n------------------------------------------------------ \nDate and Time:           2020-10-20 06:00:00 \nMin Temperature for day: 36 11.65  \nMax Temperature for day: 36 11.65 \nWeather:                 Clouds - broken clouds \nCloud %:                 65 \nWind Speed %:            0.72 \nHumidity :               62 \nIcon :                   04d \n------------------------------------------------------ \nDate and Time:           2020-10-20 09:00:00 \nMin Temperature for day: 37 14.93  \nMax Temperature for day: 37 14.93 \nWeather:                 Clouds - broken clouds \nCloud %:                 81 \nWind Speed %:            1.48 \nHumidity :               50 \nIcon :                   04d \n------------------------------------------------------ \nDate and Time:           2020-10-20 12:00:00 \nMin Temperature for day: 38 16.7  \nMax Temperature for day: 38 16.7 \nWeather:                 Clouds - scattered clouds \nCloud %:                 42 \nWind Speed %:            1.92 \nHumidity :               42 \nIcon :                   03d \n------------------------------------------------------ \nDate and Time:           2020-10-20 15:00:00 \nMin Temperature for day: 39 15.54  \nMax Temperature for day: 39 15.54 \nWeather:                 Clouds - broken clouds \nCloud %:                 65 \nWind Speed %:            1.12 \nHumidity :               45 \nIcon :                   04d \n \n2020-10-15 18:59:26 - Weather Data Downloaded \n  \n2020-10-15 18:59:26 - Weather Update Script Finished \n'),
(5, 'reboot_wifi', 'sh /var/www/cron/reboot_wifi.sh \\n', 5, '  _____    _   _    _                            \n |  __ \\  (_) | |  | |                           \n | |__) |  _  | |__| |   ___    _ __ ___     ___ \n |  ___/  | | |  __  |  / _ \\  | |_  \\_ \\   / _ \\ \n | |      | | | |  | | | (_) | | | | | | | |  __/\n |_|      |_| |_|  |_|  \\___/  |_| |_| |_|  \\___|\n \n    S M A R T   H E A T I N G   C O N T R O L \n*************************************************************************\n* PiHome is Raspberry Pi based Central Heating Control systems. It runs *\n* from web interface and it comes with ABSOLUTELY NO WARRANTY, to the   *\n* extent permitted by applicable law. I take no responsibility for any  *\n* loss or damage to you or your property.                               *\n* DO NOT MAKE ANY CHANGES TO YOUR HEATING SYSTEM UNTIL UNLESS YOU KNOW  *\n* WHAT YOU ARE DOING                                                    *\n*************************************************************************\n\n                                                           Have Fun - PiHome\nThu 15 Oct 18:59:26 EEST 2020\n - Auto Reconnect Wi-Fi Status for wlan0 Script Started \n\n*************************************************************************\nThu 15 Oct 18:59:26 EEST 2020\n\nStarting WiFi check for wlan0\n\nProcess still running, Lockfile valid\n');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `jobs`
--
ALTER TABLE `jobs`
  ADD PRIMARY KEY (`id`);

Python script to run on boot:

We need to install schedule (pip3 install schedule)

# Schedule Library imported 
import schedule 
import time 
import os
import configparser
import MySQLdb as mdb

# Initialise the database access variables
config = configparser.ConfigParser()
config.read('/var/www/st_inc/db_config.ini')
dbhost = config.get('db', 'hostname')
dbuser = config.get('db', 'dbusername')
dbpass = config.get('db', 'dbpassword')
dbname = config.get('db', 'dbname')

con = mdb.connect(dbhost, dbuser, dbpass, dbname)
cur = con.cursor()

# Functions setup 
def system_c(): 
    system = os.popen("php /var/www/cron/system_c.php \n") 
    system_output = system.read()
    #print(system_output)
    cur.execute("""UPDATE jobs SET output = %s WHERE name = 'system_c'""", [system_output])
    con.commit()

cur.execute("""SELECT `time` FROM `jobs` where `name` = 'system_c'""") 
for row in cur:
    systemtime = row[0]
    print(systemtime)

def weather_update(): 
    weather = os.popen("php /var/www/cron/weather_update.php \n")
    weather_output = weather.read()
    print(weather_output) 
    cur.execute("""UPDATE `jobs` SET `output` = %s WHERE `name` = 'weather_update'""", [weather_output])
    con.commit()

cur.execute("""SELECT `time` FROM `jobs` where `name` = 'weather_update'""")
for row in cur:
    weathertime = row[0]
    print(weathertime) 

def reboot_wifi(): 
    rwifi = os.popen("sh /var/www/cron/reboot_wifi.sh \n") 
    rwifi_output = rwifi.read()
    print(rwifi_output)
    cur.execute("""UPDATE `jobs` SET `output` = %s WHERE `name` = 'reboot_wifi'""", [rwifi_output])
    con.commit()

cur.execute("""SELECT `time` FROM `jobs` where `name` = 'reboot_wifi'""") 
for row in cur:
    reboottime = row[0]
    print(reboottime) 

def check_gw(): 
    chk_gw = os.popen("php /var/www/cron/check_gw.php \n")
    chk_gw_output = chk_gw.read()
    print(chk_gw_output)
    cur.execute("""UPDATE `jobs` SET `output` = %s WHERE `name` = 'check_gw'""", [chk_gw_output])
    con.commit()

cur.execute("""SELECT `time` FROM `jobs` where `name` = 'check_gw'""")   
for row in cur:
    checktime = row[0]
    print(checktime)

def boiler(): 
    boiler = os.popen("php /var/www/cron/boiler.php \n")
    boiler_output = boiler.read()
    print (boiler_output)
    cur.execute("""UPDATE `jobs` SET `output` = %s WHERE `name` = 'boiler'""", [boiler_output])
    con.commit()

cur.execute("""SELECT `time` FROM `jobs` where `name` = 'boiler'""") 
for row in cur:
    boilertime = row[0]
    print(boilertime)

# Task scheduling 

schedule.every(systemtime).seconds.do(system_c)
schedule.every(weathertime).seconds.do(weather_update) 
schedule.every(reboottime).seconds.do(reboot_wifi) 
schedule.every(checktime).seconds.do(check_gw) 
schedule.every(boilertime).seconds.do(boiler) 

# Loop so that the scheduling task 
# keeps on running all time. 

while True: 

    # Checks whether a scheduled task  
    # is pending to run or not 

    schedule.run_pending() 
    time.sleep(1) 

The script pulls the time (in seconds) from the database and runs the tasks repeatedly. The output of the task is inserted into the database at every run.

pihome-shc commented 3 years ago

@sandreialexandru can you attached python file with ticket, you can attached it as zip file.

sandreialexandru commented 3 years ago

I shall attach the file soon. The next step would be a user interface to update time values in the database and see the output of the scheduled scripts.

sandreialexandru commented 3 years ago

Here is my approach to running jobs at a schedule in python jobs.zip

sandreialexandru commented 3 years ago

Any thoughts on this?

pihome-shc commented 3 years ago

@sandreialexandru very interesting it takes reading from jobs table and python scripts runs as service? i think we need to implement this in a way so we can see the status under settings that it is running.

sandreialexandru commented 3 years ago

I've updated the file and the script should check itself to see if it is running. This is some kind of a paradox but if script is running it outputs the result of the bash command (pid='pgrep -f jobs.py' ; ps -o start,etimes -p $pid) to database. The result should look like this : STARTED ELAPSED 01:12:15 16 01:12:31 0 Having this output doesn't mean that the script is still running, but making another script to check the status of another one doesn't make any sense. With this information we can check if the script is running by observing the updates in the database(or in the web UI). The table "system" would need another column script_output. Feel free to edit line 83 if you think it is needed. jobs.zip

pihome-shc commented 3 years ago

@sandreialexandru very good work but its not good to have all schedules hardcoded, it would be best to get data from database and run command as is from database and do this whole think as loop rather then hard coded command in the code so if user wants to have something different or add extra cronjobs then user gets option to do this on the gui. suggested table structure.: id, record number status, (1 enable, 0 disable) name, (cosmetic name) script, php /var/www/cron/weather_update.php (full path to command and script to run) time, (how frequent to run job, (to keep it simple take this value as minutes) output_status, user can enable/disable output (1 enable, 0 disable) output, if output_status is set to 1 then save output otherwise dont save.

schedule.every(systemtime).seconds.do(system_c)
schedule.every(weathertime).seconds.do(weather_update) 
schedule.every(reboottime).seconds.do(reboot_wifi) 
schedule.every(checktime).seconds.do(check_gw) 
schedule.every(boilertime).seconds.do(boiler) 
schedule.every(5).seconds.do(script_check)
dvdcut commented 3 years ago

moved to new version (MaxAir) https://github.com/pihome-shc/PiHomeHVAC/issues/34