Closed dvdcut closed 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.
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
;
feel free to work on this, i'll try to give you my input if needed.
# 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?
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, '[36m\n _____ _ _ _ \n | __ \\ (_) | | | | \n | |__) | _ | |__| | ___ _ __ ___ ___ \n | ___/ | | | __ | / _ \\ | |_ \\_ \\ / _ \\ \n | | | | | | | | | (_) | | | | | | | | __/ \n |_| |_| |_| |_| \\___/ |_| |_| |_| \\___| \n [0m \n [45m S M A R T H E A T I N G C O N T R O L [0m \n[31m**********************************************************\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 [0m \n[36m2020-10-15 18:59:27[0m - Boiler Script Started \n[36m2020-10-15 18:59:27[0m - Day of the Week: [41m4[0m \n---------------------------------------------------------------------------------------- \n[36m2020-10-15 18:59:27[0m - Zone: Sensor Reading [41m21.60[0m \n[36m2020-10-15 18:59:27[0m - Zone: Weather Factor [41m0.6[0m \n[36m2020-10-15 18:59:27[0m - Zone: DeadBand [41m0.5[0m \n[36m2020-10-15 18:59:27[0m - Zone: Cut In Temperature [41m0[0m \n[36m2020-10-15 18:59:27[0m - Zone: Cut Out Temperature [41m0[0m \n[36m2020-10-15 18:59:27[0m - Zone: Mode [41m0[0m \n[36m2020-10-15 18:59:27[0m - Zone ID: [41m38[0m \n[36m2020-10-15 18:59:27[0m - Zone: Ground Floor Controller: [41m0[0m Controller Child: [41m23[0m Zone Status: [41m0[0m \n[36m2020-10-15 18:59:27[0m - Zone: Ground Floor Stop Cause: No Schedule - Target C:[41m0[0m Zone C:[31m21.60[0m \n---------------------------------------------------------------------------------------- \n[36m2020-10-15 18:59:27[0m - Zone: GIOP Relay Status: [41m1[0m (0=On, 1=Off) \n[36m2020-10-15 18:59:27[0m - Boiler Switched Off At: 2020-10-15 15:15:24\n[36m2020-10-15 18:59:28[0m - Boiler GIOP: [41m12[0m Status: [41m1[0m (0=On, 1=Off) \n[36m2020-10-15 18:59:28[0m - Boiler Fired Status: [41m0[0m \n[36m2020-10-15 18:59:28[0m - Boiler Hysteresis Status: [41m0[0m \n---------------------------------------------------------------------------------------- \n[36m2020-10-15 18:59:28[0m - Boiler Script Ended \n[32m****************************************************************************************[0m \n\n'),
(2, 'check_gw', 'php /var/www/cron/check_gw.php \\n', 5, '[36m\n _____ _ _ _ \n | __ \\ (_) | | | | \n | |__) | _ | |__| | ___ _ __ ___ ___ \n | ___/ | | | __ | / _ \\ | |_ \\_ \\ / _ \\ \n | | | | | | | | | (_) | | | | | | | | __/ \n |_| |_| |_| |_| \\___/ |_| |_| |_| \\___| \n [0m \n [45m S M A R T H E A T I N G C O N T R O L [0m \n[31m********************************************************\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 [0m \n[36m2020-10-15 18:59:27[0m - Python Gateway Script Status Check Script Started \n[36m2020-10-15 18:59:27[0m - Python Gateway Script for Gateway is [42mRunning[0m \n[36m2020-10-15 18:59:27[0m - The PID is: [42m935[0m \n--------------------------------------------------------------------------\n[36m2020-10-15 18:59:27[0m - Python Gateway Script Status Check Script Ended \n[32m***************************************************************************[0m\n'),
(3, 'system_c', 'php /var/www/cron/system_c.php \\n', 5, '[36m\n _____ _ _ _ \n | __ \\ (_) | | | | \n | |__) | _ | |__| | ___ _ __ ___ ___ \n | ___/ | | | __ | / _ \\ | |_ \\_ \\ / _ \\ \n | | | | | | | | | (_) | | | | | | | | __/ \n |_| |_| |_| |_| \\___/ |_| |_| |_| \\___| \n [0m \n [45m S M A R T H E A T I N G C O N T R O L [0m \n[31m********************************************************\n* System Temperature Version 0.4 Build Date 31/03/2018 *\n* Update on 30/03/2020 *\n* Have Fun - PiHome.eu *\n********************************************************\n [0m \n[36m2020-10-15 18:59:28[0m - System Temperature: 50.5\n'),
(4, 'weather_update', 'php /var/www/cron/weather_update.php \\n', 5, '[36m\n _____ _ _ _ \n | __ \\ (_) | | | | \n | |__) | _ | |__| | ___ _ __ ___ ___ \n | ___/ | | | __ | / _ \\ | |_ \\_ \\ / _ \\ \n | | | | | | | | | (_) | | | | | | | | __/ \n |_| |_| |_| |_| \\___/ |_| |_| |_| \\___| \n [0m \n [45m S M A R T H E A T I N G C O N T R O L [0m \n[31m************************************************************\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 [0m \n[36m2020-10-15 18:59:25[0m - Weather Update Script Started \n[36m2020-10-15 18:59:25[0m - Weather Data Downloaded \n[36m2020-10-15 18:59:25[0m - Current Weather Temperature 19 \n[36m2020-10-15 18:59:25[0m - Database Updated \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-15 18:00:00 \n[1;33mMin Temperature for day: [0m0 17.1 \n[1;33mMax Temperature for day: [0m0 18.03 \n[1;33mWeather: [0m Clouds - broken clouds \n[1;33mCloud %: [0m 59 \n[1;33mWind Speed %: [0m 2.02 \n[1;33mHumidity : [0m 65 \n[1;33mIcon : [0m 04n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-15 21:00:00 \n[1;33mMin Temperature for day: [0m1 15.94 \n[1;33mMax Temperature for day: [0m1 16.58 \n[1;33mWeather: [0m Clouds - few clouds \n[1;33mCloud %: [0m 16 \n[1;33mWind Speed %: [0m 1.34 \n[1;33mHumidity : [0m 66 \n[1;33mIcon : [0m 02n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-16 00:00:00 \n[1;33mMin Temperature for day: [0m2 14.79 \n[1;33mMax Temperature for day: [0m2 15.05 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 5 \n[1;33mWind Speed %: [0m 0.97 \n[1;33mHumidity : [0m 71 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-16 03:00:00 \n[1;33mMin Temperature for day: [0m3 14.05 \n[1;33mMax Temperature for day: [0m3 14.09 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 2 \n[1;33mWind Speed %: [0m 0.96 \n[1;33mHumidity : [0m 75 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-16 06:00:00 \n[1;33mMin Temperature for day: [0m4 15.39 \n[1;33mMax Temperature for day: [0m4 15.39 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 40 \n[1;33mWind Speed %: [0m 2.42 \n[1;33mHumidity : [0m 66 \n[1;33mIcon : [0m 03d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-16 09:00:00 \n[1;33mMin Temperature for day: [0m5 17.56 \n[1;33mMax Temperature for day: [0m5 17.56 \n[1;33mWeather: [0m Clouds - overcast clouds \n[1;33mCloud %: [0m 96 \n[1;33mWind Speed %: [0m 2.67 \n[1;33mHumidity : [0m 62 \n[1;33mIcon : [0m 04d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-16 12:00:00 \n[1;33mMin Temperature for day: [0m6 19.69 \n[1;33mMax Temperature for day: [0m6 19.69 \n[1;33mWeather: [0m Clouds - overcast clouds \n[1;33mCloud %: [0m 92 \n[1;33mWind Speed %: [0m 3.09 \n[1;33mHumidity : [0m 57 \n[1;33mIcon : [0m 04d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-16 15:00:00 \n[1;33mMin Temperature for day: [0m7 18.25 \n[1;33mMax Temperature for day: [0m7 18.25 \n[1;33mWeather: [0m Clouds - broken clouds \n[1;33mCloud %: [0m 52 \n[1;33mWind Speed %: [0m 1.75 \n[1;33mHumidity : [0m 64 \n[1;33mIcon : [0m 04d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-16 18:00:00 \n[1;33mMin Temperature for day: [0m8 17.11 \n[1;33mMax Temperature for day: [0m8 17.11 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 46 \n[1;33mWind Speed %: [0m 1.49 \n[1;33mHumidity : [0m 67 \n[1;33mIcon : [0m 03n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-16 21:00:00 \n[1;33mMin Temperature for day: [0m9 16.37 \n[1;33mMax Temperature for day: [0m9 16.37 \n[1;33mWeather: [0m Rain - light rain \n[1;33mCloud %: [0m 99 \n[1;33mWind Speed %: [0m 1.62 \n[1;33mHumidity : [0m 71 \n[1;33mIcon : [0m 10n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-17 00:00:00 \n[1;33mMin Temperature for day: [0m10 15.44 \n[1;33mMax Temperature for day: [0m10 15.44 \n[1;33mWeather: [0m Rain - light rain \n[1;33mCloud %: [0m 94 \n[1;33mWind Speed %: [0m 1.67 \n[1;33mHumidity : [0m 78 \n[1;33mIcon : [0m 10n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-17 03:00:00 \n[1;33mMin Temperature for day: [0m11 14.78 \n[1;33mMax Temperature for day: [0m11 14.78 \n[1;33mWeather: [0m Rain - light rain \n[1;33mCloud %: [0m 100 \n[1;33mWind Speed %: [0m 2.29 \n[1;33mHumidity : [0m 83 \n[1;33mIcon : [0m 10n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-17 06:00:00 \n[1;33mMin Temperature for day: [0m12 15.24 \n[1;33mMax Temperature for day: [0m12 15.24 \n[1;33mWeather: [0m Rain - light rain \n[1;33mCloud %: [0m 100 \n[1;33mWind Speed %: [0m 3.05 \n[1;33mHumidity : [0m 78 \n[1;33mIcon : [0m 10d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-17 09:00:00 \n[1;33mMin Temperature for day: [0m13 18.29 \n[1;33mMax Temperature for day: [0m13 18.29 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 50 \n[1;33mWind Speed %: [0m 6.46 \n[1;33mHumidity : [0m 60 \n[1;33mIcon : [0m 03d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-17 12:00:00 \n[1;33mMin Temperature for day: [0m14 20.22 \n[1;33mMax Temperature for day: [0m14 20.22 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 25 \n[1;33mWind Speed %: [0m 7.07 \n[1;33mHumidity : [0m 46 \n[1;33mIcon : [0m 03d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-17 15:00:00 \n[1;33mMin Temperature for day: [0m15 18.02 \n[1;33mMax Temperature for day: [0m15 18.02 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 0 \n[1;33mWind Speed %: [0m 6.05 \n[1;33mHumidity : [0m 46 \n[1;33mIcon : [0m 01d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-17 18:00:00 \n[1;33mMin Temperature for day: [0m16 14.72 \n[1;33mMax Temperature for day: [0m16 14.72 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 0 \n[1;33mWind Speed %: [0m 5.69 \n[1;33mHumidity : [0m 55 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-17 21:00:00 \n[1;33mMin Temperature for day: [0m17 12.76 \n[1;33mMax Temperature for day: [0m17 12.76 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 0 \n[1;33mWind Speed %: [0m 4.76 \n[1;33mHumidity : [0m 60 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-18 00:00:00 \n[1;33mMin Temperature for day: [0m18 11.38 \n[1;33mMax Temperature for day: [0m18 11.38 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 0 \n[1;33mWind Speed %: [0m 4.68 \n[1;33mHumidity : [0m 65 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-18 03:00:00 \n[1;33mMin Temperature for day: [0m19 10.38 \n[1;33mMax Temperature for day: [0m19 10.38 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 0 \n[1;33mWind Speed %: [0m 4.29 \n[1;33mHumidity : [0m 68 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-18 06:00:00 \n[1;33mMin Temperature for day: [0m20 10.76 \n[1;33mMax Temperature for day: [0m20 10.76 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 0 \n[1;33mWind Speed %: [0m 4.19 \n[1;33mHumidity : [0m 64 \n[1;33mIcon : [0m 01d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-18 09:00:00 \n[1;33mMin Temperature for day: [0m21 14.24 \n[1;33mMax Temperature for day: [0m21 14.24 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 26 \n[1;33mWind Speed %: [0m 4.24 \n[1;33mHumidity : [0m 50 \n[1;33mIcon : [0m 03d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-18 12:00:00 \n[1;33mMin Temperature for day: [0m22 16.31 \n[1;33mMax Temperature for day: [0m22 16.31 \n[1;33mWeather: [0m Clouds - broken clouds \n[1;33mCloud %: [0m 61 \n[1;33mWind Speed %: [0m 4.45 \n[1;33mHumidity : [0m 43 \n[1;33mIcon : [0m 04d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-18 15:00:00 \n[1;33mMin Temperature for day: [0m23 15.24 \n[1;33mMax Temperature for day: [0m23 15.24 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 7 \n[1;33mWind Speed %: [0m 3.31 \n[1;33mHumidity : [0m 45 \n[1;33mIcon : [0m 01d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-18 18:00:00 \n[1;33mMin Temperature for day: [0m24 12.77 \n[1;33mMax Temperature for day: [0m24 12.77 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 3 \n[1;33mWind Speed %: [0m 3.43 \n[1;33mHumidity : [0m 50 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-18 21:00:00 \n[1;33mMin Temperature for day: [0m25 10.95 \n[1;33mMax Temperature for day: [0m25 10.95 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 0 \n[1;33mWind Speed %: [0m 2.87 \n[1;33mHumidity : [0m 57 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-19 00:00:00 \n[1;33mMin Temperature for day: [0m26 9.56 \n[1;33mMax Temperature for day: [0m26 9.56 \n[1;33mWeather: [0m Clouds - few clouds \n[1;33mCloud %: [0m 12 \n[1;33mWind Speed %: [0m 2.4 \n[1;33mHumidity : [0m 63 \n[1;33mIcon : [0m 02n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-19 03:00:00 \n[1;33mMin Temperature for day: [0m27 8.89 \n[1;33mMax Temperature for day: [0m27 8.89 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 49 \n[1;33mWind Speed %: [0m 0.88 \n[1;33mHumidity : [0m 66 \n[1;33mIcon : [0m 03n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-19 06:00:00 \n[1;33mMin Temperature for day: [0m28 9.8 \n[1;33mMax Temperature for day: [0m28 9.8 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 37 \n[1;33mWind Speed %: [0m 1.03 \n[1;33mHumidity : [0m 63 \n[1;33mIcon : [0m 03d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-19 09:00:00 \n[1;33mMin Temperature for day: [0m29 13.92 \n[1;33mMax Temperature for day: [0m29 13.92 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 1 \n[1;33mWind Speed %: [0m 2.72 \n[1;33mHumidity : [0m 52 \n[1;33mIcon : [0m 01d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-19 12:00:00 \n[1;33mMin Temperature for day: [0m30 16.07 \n[1;33mMax Temperature for day: [0m30 16.07 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 1 \n[1;33mWind Speed %: [0m 2.01 \n[1;33mHumidity : [0m 44 \n[1;33mIcon : [0m 01d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-19 15:00:00 \n[1;33mMin Temperature for day: [0m31 15.13 \n[1;33mMax Temperature for day: [0m31 15.13 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 30 \n[1;33mWind Speed %: [0m 0.83 \n[1;33mHumidity : [0m 47 \n[1;33mIcon : [0m 03d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-19 18:00:00 \n[1;33mMin Temperature for day: [0m32 13.64 \n[1;33mMax Temperature for day: [0m32 13.64 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 43 \n[1;33mWind Speed %: [0m 1.18 \n[1;33mHumidity : [0m 52 \n[1;33mIcon : [0m 03n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-19 21:00:00 \n[1;33mMin Temperature for day: [0m33 11.98 \n[1;33mMax Temperature for day: [0m33 11.98 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 1 \n[1;33mWind Speed %: [0m 1.31 \n[1;33mHumidity : [0m 59 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-20 00:00:00 \n[1;33mMin Temperature for day: [0m34 10.75 \n[1;33mMax Temperature for day: [0m34 10.75 \n[1;33mWeather: [0m Clear - clear sky \n[1;33mCloud %: [0m 1 \n[1;33mWind Speed %: [0m 0.66 \n[1;33mHumidity : [0m 65 \n[1;33mIcon : [0m 01n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-20 03:00:00 \n[1;33mMin Temperature for day: [0m35 10.44 \n[1;33mMax Temperature for day: [0m35 10.44 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 34 \n[1;33mWind Speed %: [0m 0.28 \n[1;33mHumidity : [0m 66 \n[1;33mIcon : [0m 03n \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-20 06:00:00 \n[1;33mMin Temperature for day: [0m36 11.65 \n[1;33mMax Temperature for day: [0m36 11.65 \n[1;33mWeather: [0m Clouds - broken clouds \n[1;33mCloud %: [0m 65 \n[1;33mWind Speed %: [0m 0.72 \n[1;33mHumidity : [0m 62 \n[1;33mIcon : [0m 04d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-20 09:00:00 \n[1;33mMin Temperature for day: [0m37 14.93 \n[1;33mMax Temperature for day: [0m37 14.93 \n[1;33mWeather: [0m Clouds - broken clouds \n[1;33mCloud %: [0m 81 \n[1;33mWind Speed %: [0m 1.48 \n[1;33mHumidity : [0m 50 \n[1;33mIcon : [0m 04d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-20 12:00:00 \n[1;33mMin Temperature for day: [0m38 16.7 \n[1;33mMax Temperature for day: [0m38 16.7 \n[1;33mWeather: [0m Clouds - scattered clouds \n[1;33mCloud %: [0m 42 \n[1;33mWind Speed %: [0m 1.92 \n[1;33mHumidity : [0m 42 \n[1;33mIcon : [0m 03d \n[31m------------------------------------------------------[0m \n[1;33mDate and Time: [0m 2020-10-20 15:00:00 \n[1;33mMin Temperature for day: [0m39 15.54 \n[1;33mMax Temperature for day: [0m39 15.54 \n[1;33mWeather: [0m Clouds - broken clouds \n[1;33mCloud %: [0m 65 \n[1;33mWind Speed %: [0m 1.12 \n[1;33mHumidity : [0m 45 \n[1;33mIcon : [0m 04d \n \n[36m2020-10-15 18:59:26[0m - Weather Data Downloaded \n \n[36m2020-10-15 18:59:26[0m - Weather Update Script Finished \n'),
(5, 'reboot_wifi', 'sh /var/www/cron/reboot_wifi.sh \\n', 5, '[H[2J[3J _____ _ _ _ \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.
@sandreialexandru can you attached python file with ticket, you can attached it as zip file.
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.
Here is my approach to running jobs at a schedule in python jobs.zip
Any thoughts on this?
@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.
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
@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)
moved to new version (MaxAir) https://github.com/pihome-shc/PiHomeHVAC/issues/34
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?