Skip to content

Commit 005471d

Browse files
authored
A script to notify when a process finishes execution (#169)
1 parent 89feb5f commit 005471d

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ More information on contributing and the general code of conduct for discussion
7979
| Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/planetSimulation) | A simulation of several planets rotating around the sun. |
8080
| Remove Background | [Remove Background](https://github.com/DhanushNehru/Python-Scripts/tree/master/Remove%20Background) | Removes the background of images. |
8181
| ROCK-PAPER-SCISSOR | [ROCK-PAPER-SCISSOR](https://github.com/DhanushNehru/Python-Scripts/tree/master/ROCK-PAPER-SCISSOR) | A game of Rock Paper Scissors. |
82+
| Run Then Notify | [Run Then Notify](https://github.com/DhanushNehru/Python-Scripts/tree/master/Run%20Then%20Notify) | Runs a slow command and mails you when it completes execution. |
8283
| Selfie with Python | [Selfie_with_Python](https://github.com/DhanushNehru/Python-Scripts/tree/master/Selfie_with_Python) | Take your selfie with python . |
8384
| Simple TCP Chat Server | [Simple TCP Chat Server](https://github.com/DhanushNehru/Python-Scripts/tree/master/TCP%20Chat%20Server) | Creates a local server on your LAN for receiving and sending messages! |
8485
| Snake Water Gun | [Snake Water Gun](https://github.com/DhanushNehru/Python-Scripts/tree/master/Snake-Water-Gun) | A game similar to Rock Paper Scissors. |

Run Then Notify/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Run then Notify
2+
3+
This is a python script that will run any terminal command on a system and once the command successfully runs, it will notify you via email of its time taken and the exit code.
4+
5+
This can be useful in case of long running commands.
6+
7+
Please enter your email and password when prompted. These values are _not_ stored anywhere and are only used for the SMTP connection.
8+
9+
> This system has a hardcoded gmail smtp server specified. In case of any other smtp server, please update the value in code.
10+
>
11+
> **Note**: You may have to enable `less secure app access` in your gmail account.

Run Then Notify/run_then_notify.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import subprocess
2+
import sys
3+
import time
4+
import socket
5+
from getpass import getpass
6+
import smtplib
7+
from email.mime.text import MIMEText
8+
from email.mime.multipart import MIMEMultipart
9+
10+
11+
user_email = input("Enter your email: ")
12+
user_password = getpass()
13+
smtp_server = "smtp.gmail.com"
14+
smtp_port = 587 # Port 587 for TLS
15+
16+
17+
def send_text_email(subject, message, recipient_email, sender_email, sender_password, smtp_server, smtp_port):
18+
# Create a text/plain message
19+
msg = MIMEMultipart()
20+
msg['From'] = sender_email
21+
msg['To'] = recipient_email
22+
msg['Subject'] = subject
23+
24+
# Attach the text message
25+
msg.attach(MIMEText(message, 'plain'))
26+
27+
# Connect to the SMTP server
28+
try:
29+
server = smtplib.SMTP(smtp_server, smtp_port)
30+
server.starttls()
31+
server.login(sender_email, sender_password)
32+
text = msg.as_string()
33+
server.sendmail(sender_email, recipient_email, text)
34+
server.quit()
35+
except Exception as e:
36+
print(f"Email could not be sent: {str(e)}")
37+
38+
39+
def run_then_notify(command):
40+
try:
41+
start_time = time.time()
42+
completed_process = subprocess.run(
43+
command,
44+
text=True,
45+
shell=True,
46+
stdout=sys.stdout,
47+
stderr=subprocess.STDOUT,
48+
stdin=sys.stdin
49+
)
50+
51+
end_time = time.time()
52+
execution_time = end_time - start_time
53+
54+
notif_subject = '[Run completed] Your command finished execution'
55+
notif_message = f"Command '{command}' completed\n\
56+
\twith exit code {completed_process.returncode}\n\
57+
\tin {execution_time:.2f} seconds\n\
58+
\n\
59+
This is an automated message sent from your device {socket.gethostname()}"
60+
61+
send_text_email(
62+
subject=notif_subject,
63+
message=notif_message,
64+
recipient_email=user_email,
65+
sender_email=user_email,
66+
sender_password=user_password,
67+
smtp_server=smtp_server,
68+
smtp_port=smtp_port
69+
)
70+
71+
except Exception as e:
72+
print(f"Error: {e}")
73+
74+
75+
if __name__ == "__main__":
76+
if len(sys.argv) != 2:
77+
print("Usage: python run_then_notify.py 'your_command_here'")
78+
sys.exit(1)
79+
80+
command = sys.argv[1]
81+
run_then_notify(command)

0 commit comments

Comments
 (0)