-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.py
73 lines (57 loc) · 1.76 KB
/
watcher.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
This script will wait for executing the <command> until the condition is achieved.
Usage: python watch.py <type>:<target> "<command> [--gap <seconds>] [--reverse]"
For example:
1. Waiting until the target file exists:
# python watch.py f:test "ls -l" --gap 1
2. Waiting until the process with pid 1 disappears:
# python watch.py p:1 "ls -l" --gap 1 --reverse
"""
import argparse
import os
import subprocess
import signal
import time
def main(args):
print("Waiting to run: \n# {}".format(args.command))
print("Checking {} every {} seconds...".format(args.target, args.gap))
try:
_type, value = args.target.split(":")
except:
raise NotImplementedError("The format of target is <type>:<value>")
while True:
if _type in ["f", "file"]:
result = os.path.isfile(value)
elif _type in ["p", "pid"]:
try:
os.kill(int(value), 0)
except OSError:
result = False
else:
result = True
else:
raise NotImplementedError("Unknown type: {}.".format(_type))
if result ^ args.reverse:
execute(args.command)
break
time.sleep(args.gap)
def execute(command):
print(command)
print()
p = subprocess.Popen(command, shell=True)
try:
p.wait()
except KeyboardInterrupt:
try:
os.kill(p.pid, signal.SIGINT)
except OSError:
pass
p.wait()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("target")
parser.add_argument("command")
parser.add_argument("--gap", default=300, type=int)
parser.add_argument("--reverse", action="store_true")
args = parser.parse_args()
main(args)