-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_crontab.py
69 lines (57 loc) · 2.26 KB
/
init_crontab.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
import argparse
import os
import sys
from crontab import CronTab
def add_cron_job(interval, command, log_file):
# User's crontab
cron = CronTab(user=True)
# Check if the cron job already exists
job_exists = False
for job in cron:
if job.command == command:
job_exists = True
break
# If the job does not exist, add it
if not job_exists:
job = cron.new(command=command, comment='Communicator data processing job')
# Set the job interval based on user input
if interval == 'minute':
job.minute.every(1)
elif interval == 'hourly':
job.hour.every(1)
elif interval == 'daily':
job.hour.every(24)
elif interval == 'weekly':
job.dow.on('SUN')
elif interval == 'monthly':
job.day.on(1)
else:
print('Invalid interval specified.')
return
cron.write()
print(f'Cron job added to run {interval}.')
else:
print('Cron job already exists.')
def ensure_communicator_user():
if os.geteuid() != 0:
if os.getlogin() != 'campusenergy':
print("This script must be run by the 'communicator' user.")
sys.exit(1)
def ensure_log_directory(log_file):
log_dir = os.path.dirname(log_file)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
print(f'Created log directory: {log_dir}')
else:
print(f'Log directory already exists: {log_dir}')
if __name__ == '__main__':
ensure_communicator_user()
parser = argparse.ArgumentParser(description='Add a cron job to run a script at a specified interval.')
parser.add_argument('interval', type=str, choices=['minute', 'hourly', 'daily', 'weekly', 'monthly'],
help='Interval at which to run the cron job (minute, hourly, daily, weekly, monthly)')
parser.add_argument('script_path', type=str, help='Path to the script to be run by the cron job')
parser.add_argument('log_file', type=str, help='Path to the log file')
args = parser.parse_args()
ensure_log_directory(args.log_file)
command = f'/usr/bin/python3 {args.script_path} >> {args.log_file} 2>&1'
add_cron_job(args.interval, command, args.log_file)