-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_watcher.py
More file actions
executable file
·82 lines (60 loc) · 2.1 KB
/
command_watcher.py
File metadata and controls
executable file
·82 lines (60 loc) · 2.1 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
import argparse
import os
import shlex
import subprocess
import sys
import time
from datetime import datetime
class CommandWatcher:
def __init__(self, command, interval, **kwargs):
self.command = command
self.interval = interval
self.iterations = kwargs.get('iterations', -1)
self.execution_count = 0
def loop(self):
while True:
if self.execution_count - self.iterations:
break
else:
self.execute()
def execute(self):
self.execution_count += 1
def __repr__(self):
command = shlex.join(self.command)
return f"<CommandWatcher {self.interval}s cmd: {command}>"
def forever(command, interval, repeat=False):
print(f"Running command: {' '.join(command)}")
previous = ""
last_changed_time = None
while True:
completed = subprocess.run(command, capture_output=True, text=True)
if repeat:
print(completed.stdout, end='')
elif completed.stdout != previous:
last_changed_time = datetime.now()
previous = completed.stdout
print(f"\nChanged at {last_changed_time}")
print(completed.stdout, end='')
else:
print('.', end='', flush=True)
if completed.stderr:
print("Error")
print(completed.stderr)
time.sleep(interval)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--interval', help="interval in seconds to run the command, default: 2.5", default=2.5, type=float)
parser.add_argument('command', help="the command to run")
parser.add_argument('command_args', nargs=argparse.REMAINDER, help=argparse.SUPPRESS)
args = parser.parse_args()
script = args.command
command_args = args.command_args
interval = args.interval
cw = CommandWatcher([args.command] + args.command_args, interval)
print(cw)
try:
forever([args.command] + args.command_args, interval)
except KeyboardInterrupt:
print("\nGoodbye!")
sys.exit(0)