-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrepeat_command
More file actions
executable file
·73 lines (67 loc) · 1.75 KB
/
repeat_command
File metadata and controls
executable file
·73 lines (67 loc) · 1.75 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
#!/bin/bash
# Default values
OUTPUT_FILE=~/tmp/repeated.log
MAX_REPEAT=100
INTERVAL=600 # 10 minutes in seconds
usage() {
echo "Usage: $0 [OPTIONS] <command>"
echo ""
echo "Options:"
echo " --out=<file> Set the output file to append results to (default: ~/tmp/amd.log)"
echo " -n=<count> Set the number of times the command should be repeated (default: 100)"
echo " --interval=<secs> Set the interval between command executions in seconds (default: 600)"
echo " -h Show this help message and exit"
echo ""
echo "Example:"
echo " $0 --out=\"/home/myself/docs/123.tmp\" -n=25 --interval=1000 \"history | grep 123\""
echo " $0 --out=\"/home/ilya/tmp/speedtest.log\" -n=100 --interval=300 \"speedtest-cli\""
}
# Parse options
while [ "$#" -gt 0 ]; do
case "$1" in
--out=*)
OUTPUT_FILE="${1#*=}"
shift
;;
-n=*)
MAX_REPEAT="${1#*=}"
shift
;;
--interval=*)
INTERVAL="${1#*=}"
shift
;;
-h)
usage
exit 0
;;
*)
if [ -z "$COMMAND" ]; then
COMMAND="$1"
shift
else
echo "Error: Invalid option or too many arguments"
echo ""
usage
exit 1
fi
;;
esac
done
# Check if the command is provided
if [ -z "$COMMAND" ]; then
echo "Error: Missing command"
echo ""
usage
exit 1
fi
# Create the output file directory if it doesn't exist
mkdir -p "$(dirname "$OUTPUT_FILE")"
# Run the command and append the result to the output file
COUNTER=0
while [ $COUNTER -lt $MAX_REPEAT ]; do
echo -e "\n------ $(date) --- ITERATION ${COUNTER} --- COMMAND ${COMMAND} ------\n" | tee -a "$OUTPUT_FILE"
eval "$COMMAND" | tee -a "$OUTPUT_FILE"
sleep $INTERVAL
COUNTER=$((COUNTER + 1))
done