-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron_manager.py
More file actions
118 lines (93 loc) · 3.73 KB
/
Copy pathcron_manager.py
File metadata and controls
118 lines (93 loc) · 3.73 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Cron job management module.
Provides functions to list, create, and delete crontab entries
for the current user. All write operations require user confirmation.
"""
import subprocess
def cron_list() -> str:
"""List all current crontab entries."""
result = subprocess.run(["crontab", "-l"], capture_output=True, text=True)
if result.returncode != 0:
if "no crontab" in result.stderr.lower():
return "No crontab entries found."
return f"Error reading crontab: {result.stderr}"
lines = result.stdout.strip().split("\n")
if not lines or lines == [""]:
return "No crontab entries found."
output = ["Current crontab entries:\n"]
for i, line in enumerate(lines):
prefix = f" [{i}] " if not line.startswith("#") else f" [#] "
output.append(f"{prefix}{line}")
return "\n".join(output)
def cron_create(schedule: str, command: str) -> str:
"""Add a new cron job after user confirmation.
Args:
schedule: Cron schedule expression (e.g. '0 9 * * *' for daily at 9am)
command: Shell command to execute
"""
entry = f"{schedule} {command}"
print(f"\n{'='*60}")
print(f"CRON JOB CONFIRMATION")
print(f"{'='*60}")
print(f" Schedule: {schedule}")
print(f" Command: {command}")
print(f" Full entry: {entry}")
print(f"{'='*60}")
try:
confirm = input("Add this cron job? [y/N]: ").strip().lower()
except (EOFError, KeyboardInterrupt):
return "Cancelled by user."
if confirm != "y":
return "Cron job creation cancelled by user."
# Get existing crontab
result = subprocess.run(["crontab", "-l"], capture_output=True, text=True)
existing = result.stdout if result.returncode == 0 else ""
# Append new entry
new_crontab = existing.rstrip("\n") + "\n" + entry + "\n"
result = subprocess.run(
["crontab", "-"], input=new_crontab, capture_output=True, text=True
)
if result.returncode != 0:
return f"Error creating cron job: {result.stderr}"
return f"Cron job created: {entry}"
def cron_delete(index: int) -> str:
"""Delete a cron job by its index number after user confirmation.
Args:
index: The index of the cron job to delete (from cron_list output)
"""
result = subprocess.run(["crontab", "-l"], capture_output=True, text=True)
if result.returncode != 0:
return "No crontab entries to delete."
lines = result.stdout.strip().split("\n")
# Build index mapping (only non-comment lines get numeric indices)
indexed = []
for line in lines:
if not line.startswith("#"):
indexed.append(line)
if index < 0 or index >= len(indexed):
return f"Invalid index {index}. Valid range: 0-{len(indexed)-1}"
target = indexed[index]
print(f"\n{'='*60}")
print(f"CRON JOB DELETE CONFIRMATION")
print(f"{'='*60}")
print(f" Deleting [{index}]: {target}")
print(f"{'='*60}")
try:
confirm = input("Delete this cron job? [y/N]: ").strip().lower()
except (EOFError, KeyboardInterrupt):
return "Cancelled by user."
if confirm != "y":
return "Cron job deletion cancelled by user."
# Remove the target line
remaining = [line for line in lines if line != target]
new_crontab = "\n".join(remaining) + "\n" if remaining else ""
if new_crontab.strip():
result = subprocess.run(
["crontab", "-"], input=new_crontab, capture_output=True, text=True
)
else:
result = subprocess.run(["crontab", "-r"], capture_output=True, text=True)
if result.returncode != 0:
return f"Error deleting cron job: {result.stderr}"
return f"Cron job deleted: {target}"
if __name__ == "__main__":
print(cron_list())