From 29beed226ff752d4b4f55a11585cfe35f87128ac Mon Sep 17 00:00:00 2001 From: WickedYoda <68271168+wickedyoda@users.noreply.github.com> Date: Tue, 22 Aug 2023 14:55:28 -0500 Subject: [PATCH] Updated setting up cronjob auto updates adding section to install python3 if not already installed. --- python_scripts/setup_cron_jobs.py | 20 --------------- python_scripts/setup_cronjob.py | 42 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 python_scripts/setup_cronjob.py diff --git a/python_scripts/setup_cron_jobs.py b/python_scripts/setup_cron_jobs.py index 2ce7586..e69de29 100644 --- a/python_scripts/setup_cron_jobs.py +++ b/python_scripts/setup_cron_jobs.py @@ -1,20 +0,0 @@ -import subprocess - -# Run 'sudo apt update' -subprocess.run(['sudo', 'apt', 'update']) - -# Run 'sudo apt install cron cron-apt -y' -subprocess.run(['sudo', 'apt', 'install', 'cron', 'cron-apt', '-y']) - -# Run 'sudo systemctl enable cron' -subprocess.run(['sudo', 'systemctl', 'enable', 'cron']) - -# Run 'sudo systemctl stop cron' -subprocess.run(['sudo', 'systemctl', 'stop', 'cron']) - -# Append the cron job to /etc/crontab -cron_job = 'echo "MAILTO=\"alerts@tyates.one\"\n0 */6 * * * root apt-get update && apt-get -y -d full-upgrade && apt-get autoremove -y && sudo apt-get clean -y && sudo apt-get purge -y" >> /etc/crontab' -subprocess.run(cron_job, shell=True) - -# Run 'sudo systemctl start cron' -subprocess.run(['sudo', 'systemctl', 'start', 'cron']) diff --git a/python_scripts/setup_cronjob.py b/python_scripts/setup_cronjob.py new file mode 100644 index 0000000..79f14ff --- /dev/null +++ b/python_scripts/setup_cronjob.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import subprocess + +#!/usr/bin/env python3 + +import subprocess + +# Check if Python 3 is installed +try: + subprocess.run(["python3", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True) +except subprocess.CalledProcessError: + print("Python 3 is not installed. Installing Python 3...") + subprocess.run(["sudo", "apt", "update"]) + subprocess.run(["sudo", "apt", "install", "python3", "-y"]) + +# Rest of the script as before +# ... + +# Update package lists and install cron +subprocess.run(["sudo", "apt", "update"]) +subprocess.run(["sudo", "apt", "install", "cron", "-y"]) + +# Enable and start the cron service +subprocess.run(["sudo", "systemctl", "enable", "cron"]) +subprocess.run(["sudo", "systemctl", "start", "cron"]) + +# Stop the cron service to make changes +subprocess.run(["sudo", "systemctl", "stop", "cron"]) + +# Define the cron job command +cron_job_command = ( + 'MAILTO="alerts@tyates.one"\n' + '0 */6 * * * root apt-get update && apt-get -y -d full-upgrade && apt-get autoremove -y && sudo apt-get clean -y && sudo apt-get purge -y' +) + +# Write the cron job to a file +with open("/etc/cron.d/auto_updates", "w") as cron_file: + cron_file.write(cron_job_command) + +# Restart the cron service to apply the new cron job +subprocess.run(["sudo", "systemctl", "start", "cron"])