ein Python-Script, welches im Terminal ohne Probleme ausgeführt wird, wird als Cronjob vollständig ignoriert.
Hier der Code des Scripts:
Code: Alles auswählen
import logging
logging.basicConfig(filename='/home/user/checkUpdates.log', level=logging.DEBUG)
try:
import smtplib
from cryptography.fernet import Fernet
from pathlib import Path
from subprocess import run
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
if Path(".key.key").exists():
key = ""
encrypted_password = ""
decrypted_password = ""
cipher_suite = ""
command = ""
with open(".key.key", "rb") as key_file:
key = key_file.read()
cipher_suite = Fernet(key)
with open(".encrypted_password.txt", "rb") as file:
encrypted_password = file.read()
# Descypt password
decrypted_password = cipher_suite.decrypt(encrypted_password).decode("utf-8")
command = "sudo apt update && sudo apt list --upgradable > /home/user/mail.txt".format(decrypted_password)
run(command, shell=True, capture_output=True, text=True)
# E-Mail
from_email = "xxx@xxx.de"
to_email = "xxx@xxx.de"
subject = "STATUS SRV - Updates verfügbar"
body = ""
with open("mail.txt", "r") as file:
body = file.read()
# SMTP
smtp_server = "xxx.xxx.xxx"
smtp_port = 25
# Create E-Mail
msg = MIMEMultipart()
msg["From"] = from_email
msg["To"] = to_email
msg["Subject"] = subject
msg.attach(MIMEText(body, "plain"))
# Send E-Mail
with smtplib.SMTP(smtp_server, smtp_port) as server:
# server.login(smtp_user, smtp_password)
server.send_message(msg)
# Delete file
Path("mail.txt").unlink()
logging.info("Update command executed successfully.")
except Exception as e:
logging.error(f"Error executing update command: {e}")
Code: Alles auswählen
15 15 * * * /usr/bin/python3 /home/user/checkUpdates.py
LG McProgger