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
|
#!/usr/bin/env python3
######################
import os, subprocess
import argparse
import time
############################################################################################################
def defineArgs():
parser = argparse.ArgumentParser(description="Continuously watch and display the output of a command.")
parser.add_argument(
"-c", "--command",
type=str,
default='printf "Default \\"cwatch\\" command.\nCommand test, using \\"date\\" command within \\"printf\\":\n\n$(date)"',
help="The command to execute and watch."
)
parser.add_argument(
"-i", "--interval",
type=float,
default=1.0, # Default interval in seconds
help="Time interval between command executions (in seconds, default: 1.0)."
)
parser.add_argument(
"-d", "--dClear",
action="store_true", # Makes it a boolean flag, True if present, False otherwise
help="Don't clear the screen before each command output."
)
return parser.parse_args()
def captureCommandOutput(command):
output = subprocess.run(
['script', '-qec', command, '/dev/null'],
capture_output=True,
text=True,
check=True
)
return output.stdout
#################################
args = defineArgs()
command = args.command
interval = args.interval
clear = args.dClear
if clear == True:
clear = False
else:
clear = True
output = ""
error = ""
############################################################
while True:
try:
output = captureCommandOutput(command)
error = ""
except Exception as errorCode:
error = f"\nError for this iteration: {errorCode}\n"
if clear:
os.system("clear")
print(output)
print(error, end="")
time.sleep(interval)
|