How to Schedule a Python Script Reminder for System Health Check?

1. Create a Python Script

How to Schedule a Python Script Reminder for System Health Check? This guide will show you how to create python script reminder for system health check.

Create a python file

cd ~
vi dailycheck.py

Edit assigned_name and assigned_email

Edit assigned_name and assigned_email depend on your team members. For example:

assigned_name = "Jackson"
assigned_email = "[email protected]"
Then setting SMTP mail 

email_sender_account = "[email protected]" #your email

email_sender_username = "[email protected]" #your email username

email_sender_password = "mypassword"#your email password

email_smtp_server = "smtp.gmail.com" #change if not gmail.

email_smtp_port = 587 #change if needed.

email_recepients = [assigned_email,"[email protected]"] #your receipts

The entire script will look like this:

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from datetime import date



### Caculation

assigned_name = ""

assigned_email = ""

start_date = date(2000, 1, 1)

today = date.today()



delta = today - start_date



if delta.days % 5 == 0:

assigned_name = "Jackson"

assigned_email = "[email protected]"

if delta.days % 5 == 1:

assigned_name = "Mal"

assigned_email = "[email protected]"

if delta.days % 5 == 2:

assigned_name = "Alley"

assigned_email = "[email protected]"

if delta.days % 5 == 3:

assigned_name = "Bob"

assigned_email = "[email protected]"

if delta.days % 5 == 4:

assigned_name = "Thomas"

assigned_email = "[email protected]"



email_sender_account = "[email protected]" #your email

email_sender_username = "[email protected]" #your email username

email_sender_password = "mypassword"#your email password

email_smtp_server = "smtp.gmail.com" #change if not gmail.

email_smtp_port = 587 #change if needed.

email_recepients = [assigned_email,"[email protected]"] #your receipts







def SendEmail (assigned_name,today):

email_subject = f"{assigned_name} - Please Do Daily Health Check"

email_body = '<html><head></head><body>'

email_body += '<style type="text/css"></style>'

email_body += '<h2>Daily Health Check Reminder</h2>'

#Performer

email_body += f'<h1 style="color: rgb(207, 29, 29);">'

email_body += f'<b>Performer</b>: '

email_body += f'{assigned_name}</h1>'

#Date

email_body += f'<h2 style="color: rgb(96, 136, 247);">'

email_body += f'<b>Date</b>: '

email_body += f'{today}</h2>'



#Link

email_body += '<h2><a href="https://google.com">Click Here To View Schedule</a> '

email_body += '</h2>'



#footer

email_body += '<br>Reminded By'

email_body += '<br>PYTHON SCRIPT</body></html>'

server = smtplib.SMTP(email_smtp_server,email_smtp_port)

print(f"Logging in to {email_sender_account}")

server.starttls()

server.login(email_sender_username, email_sender_password)

for recipient in email_recepients:

print(f"Sending email to {recipient}")

message = MIMEMultipart('alternative')

message['From'] = email_sender_account

message['To'] = recipient

message['Subject'] = email_subject

message.attach(MIMEText(email_body, 'html'))

server.sendmail(email_sender_account,recipient,message.as_string())

server.quit()



SendEmail(assigned_name,today)

print(f"End")

2. Add this script to crontab

The script must be executable so your cron service can run it. To give executable permission to your script, you can use chmod command. For example:
sudo chmod +x dailycheck.py

List all available time zones

timedatectl list-timezones
sudo timedatectl set-timezone your_time_zone

For example, to change the system’s timezone to Europe/Rome:

sudo timedatectl set-timezone Europe/Rome

Run the timedatectl command to verify the changes:

timedatectl
                      Local time: Thu 2021-01-24 00:27:43 CET
                  Universal time: Wed 2021-01-23 23:27:43 UTC
                        RTC time: Wed 2021-01-23 23:27:44
                       Time zone: Europe/Rome (CET, +0100)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no
crontab -e
Add this line at the end of the file
0 7 * * * /usr/bin/python3 /home/jackson/dailycheck.py >> ~/cron.log 2>&1
sudo systemctl restart cron

You can find more Python Scripts in here:

https://hijackson.com/category/automation/python-scripts/

Leave a Reply

Your email address will not be published.

How to use Python Virtualenv and Virtualenvwrapper on Windows

How To Set Never Expired Password For vCenter SSO Users?

How to Setup RabbitMQ Cluster for vCloud Director 9.7?

How to Install Docker Compose on Ubuntu 22.04

How to Generate Let’s Encrypt SSL Wildcard using Certbot?

How to mount NFS as a service?

How To Upgrade Zabbix Server 4.0.X To 5.0.X On Centos 7?

How to Install Docker on Ubuntu 22.04 Quickly