What's new

[Python] Server Restarter + Mailer Notify

5
2023
1
Python:
import subprocess
import time
import psutil
import os
from datetime import datetime
import smtplib
from email.mime.text import MIMEText

def is_process_running(process_name):
    for process in psutil.process_iter(['pid', 'name']):
        if process.info['name'] == process_name:
            return True
    return False

def send_email(smtp_server, smtp_port, sender_email, sender_password, recipient_email, subject, body):
    message = MIMEText(body)
    message['Subject'] = subject
    message['From'] = sender_email
    message['To'] = recipient_email

    try:
        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(sender_email, sender_password)
            server.sendmail(sender_email, recipient_email, message.as_string())
        print("Email sent successfully.")
    except Exception as e:
        print(f"Error sending email: {e}")

def restart_server(process_name, script_directory, startup_delay):
    smtp_server = 'smtp-mail.outlook.com' # Leave alone
    smtp_port = 587 # Leave alone
    sender_email = 'your_sender_email@hotmail.com' # Outlook email
    sender_password = 'your_sender_password'  # Use Outlook Password
    recipient_email = 'your_recipient_email@example.com' # Where it's sending to.

    try:
        while True:
            if not is_process_running(process_name):
                current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                message = f"{current_time} - {process_name} is not running. Restarting..."
                print(message)

               # Send Email
                send_email(
                    smtp_server,
                    smtp_port,
                    sender_email,
                    sender_password,
                    recipient_email,
                    "Server Restarted",
                    message
                )

                subprocess.Popen(['start', process_name], cwd=script_directory, shell=True)
                time.sleep(startup_delay)

                print(f"{process_name} restarted successfully.")

            time.sleep(check_interval)

    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    process_name = 'worldserver.exe'
    script_directory = os.path.dirname(os.path.abspath(__file__))
    startup_delay = 10
    check_interval = 60

    restart_server(process_name, script_directory, startup_delay)


I'd suggest using a Outlook account.
put the Python script in the same folder as the worldserver.exe
You can adjust startup delay, and check interval at the bottom. Defaults are 10s, and 60s.


This will constantly check if the server is down every minute. If worldserver.exe is not running it will restart the program, and then send a email to you letting you know server restarted for whatever reason.

I'm not at my server box all the time, and have been having some crashing issues as of late. figured id share this for anyone needing something similar
 
Top