Fix crontab on systems with crontab

This commit is contained in:
Tim Van Baak 2024-11-06 21:27:48 -08:00
parent 8337b74a80
commit 718220f889
1 changed files with 6 additions and 7 deletions

View File

@ -1,5 +1,6 @@
from pathlib import Path from pathlib import Path
import os import os
import shutil
import subprocess import subprocess
import sys import sys
@ -28,15 +29,13 @@ def update_crontab_entries(data_path: Path):
Update the intake-managed section of the user's crontab. Update the intake-managed section of the user's crontab.
""" """
# If there is no crontab command available, quit early. # If there is no crontab command available, quit early.
cmd = ("command", "-v", "crontab") crontab = shutil.which("crontab")
print("Executing", *cmd, file=sys.stderr) if not crontab:
crontab_exists = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE) print("No crontab found, skipping", file=sys.stderr)
if not crontab_exists.stdout:
print("Could not update crontab", file=sys.stderr)
return return
# Get the current crontab # Get the current crontab
cmd = ["crontab", "-e"] cmd = [crontab, "-e"]
print("Executing", *cmd, file=sys.stderr) print("Executing", *cmd, file=sys.stderr)
get_crontab = subprocess.run( get_crontab = subprocess.run(
cmd, cmd,
@ -77,7 +76,7 @@ def update_crontab_entries(data_path: Path):
print("Updating", len(new_crontab_lines) - 2, "crontab entries", file=sys.stderr) print("Updating", len(new_crontab_lines) - 2, "crontab entries", file=sys.stderr)
# Save the updated crontab # Save the updated crontab
cmd = ["crontab", "-"] cmd = [crontab, "-"]
print("Executing", *cmd, file=sys.stderr) print("Executing", *cmd, file=sys.stderr)
new_crontab: bytes = "\n".join(new_crontab_lines).encode("utf8") new_crontab: bytes = "\n".join(new_crontab_lines).encode("utf8")
save_crontab = subprocess.Popen( save_crontab = subprocess.Popen(