Monitoring tasks on server

Since I can’t determine when the tasks on the server will finish, and it’s impossible to constantly monitor the server to check whether the tasks are completed, I need a way to notify me when task is finished.

In the end, I use a solution which is combined by Bash and yagmail.

Simply put, I write a Python file to send email to me, then, execute this Python file at the end of the Bash script that runs the task.

Here’s an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import argparse

import yagmail


def init_parse():
parse = argparse.ArgumentParser()
parse.add_argument('-d', '--description', type=str, default='')

return parse


if __name__ == "__main__":
user_name = os.getlogin()
server_name = os.uname()[1]
parse = init_parse()
args = parse.parse_args()

try:
mail_server = yagmail.SMTP(
user="sender_address",
password="SMTP_password",
port=587,
smtp_starttls=True,
smtp_ssl=None,
)
email_address = ["recipient_address"]
title = [f"{args.description} experiment on {server_name} finished"]
content = [" "]
mail_server.send(to=email_address, subject=title, contents=content)
mail_server.close()
print("sending finished")
except Exception as e:
print(repr(e))

Bash scripts:

1
2
3
4
5
6

# Your task
python xxxx

# Notification
python send_email.py -d xxxx