-
Notifications
You must be signed in to change notification settings - Fork 3
/
initcron.py
78 lines (66 loc) · 2.56 KB
/
initcron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
# *****************************************
# irrigator - Initialize Crontab
# *****************************************
#
# Description: Add cron entries for items
#
# 1. Add reboot script to initialize relays
# @reboot cd /usr/local/bin/irrigator && sudo python3 control.py -i &
#
# 2. Add monthly log cleanup
# 0 0 1 * * cd /usr/local/bin/irrigator/logs && sh backup.sh
#
# 3. Add periodic weather check
# */15 * * * * cd /usr/local/bin/irrigator && sudo python3 openwx.py
#
# *****************************************
from crontab import CronTab
from common import *
def checkexists(comment_string, system_cron):
count = 0 # Init count variable
for count in system_cron.find_comment(comment_string):
print(count) # Look for finding the CRON Job
return(count)
# Main Start
system_cron = CronTab(user='root')
# Initialize Relays on Boot
command_string = "cd /usr/local/bin/irrigator && sudo python3 control.py -i &"
comment_string = "Irrigator-Init"
if (checkexists(comment_string, system_cron)==0):
# If not found, create it.
entry = system_cron.new(command=command_string,comment=comment_string)
entry.every_reboot()
entry.enable()
system_cron.write()
# Monthly Log Cleanup
command_string = "cd /usr/local/bin/irrigator/logs && sh backup.sh"
comment_string = "Irrigator-Log"
if (checkexists(comment_string, system_cron)==0):
# If not found, create it.
entry = system_cron.new(command=command_string,comment=comment_string)
entry.setall("0 0 1 * *")
entry.enable()
system_cron.write()
# Cache weather information every 15 minutes
command_string = "cd /usr/local/bin/irrigator && sudo python3 openwx.py"
comment_string = "Irrigator-WxCache"
if (checkexists(comment_string, system_cron)==0):
# If not found, create it.
entry = system_cron.new(command=command_string,comment=comment_string)
entry.minute.every(15)
entry.enable()
system_cron.write()
# Add schedules to the crontab
json_data_dict = ReadJSON() # This will create default schedules if none exist
for item in json_data_dict['schedules']:
command_string = "cd /usr/local/bin/irrigator && sudo python3 control.py -s " + item + " &"
if (checkexists(item, system_cron)==0):
# If not found, create it.
entry = system_cron.new(command=command_string,comment=item)
entry.setall(json_data_dict['schedules'][item]['start_time']['cron_string'])
if(json_data_dict['schedules'][item]['start_time']['enabled']):
entry.enable()
else:
entry.enable(False)
system_cron.write()