forked from 1a1a11a/taskSubmitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.py
More file actions
executable file
·57 lines (39 loc) · 1.54 KB
/
watcher.py
File metadata and controls
executable file
·57 lines (39 loc) · 1.54 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
"""
this is a python module monitoring single process and notify user
when the process disappear (either finished or crashed)
it loads config information from $HOME/.task_sumitter
"""
import os
import sys
import time
import socket
import configparser
try:
import psutil
except:
print("unable to start monitoring service, please install psutil using pip3", file=sys.stderr)
sys.exit(1)
sys.path.append(os.getcwd())
sys.path.append("./")
from utilEmail import configEmailClient
def watch(pid, name="no name", check_interval=1):
""" check the given process has finished or not
if it finishes, then send out email notification
:param pid: the pid of monitoring process
:param name: the name of process, this is optional, only used for notification
:param check_interval: how long the monitoring process should wait before next check
:return: None
"""
msg = 'task “ {} ({}) ” on _{}_ just began executing! ✌'.format(name, pid, socket.gethostname())
configEmailClient().send_email(message=msg, topic="Task Submitted")
while psutil.pid_exists(pid):
time.sleep(check_interval)
msg = 'task “ {} ({}) ” on _{}_ finished executing ✋'.format(name, pid, socket.gethostname())
configEmailClient().send_email(message=msg, topic="Task Finished")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("usage: python3 {} pid name(optional)".format(sys.argv[0]))
else:
name=" ".join(sys.argv[2:]) if len(sys.argv)>2 else "no name"
watch(int(sys.argv[1]), name)