|
1 | 1 | #!/usr/bin/env bash |
2 | | -# Usage: watch.sh [cfg file] [app and command....] |
| 2 | +# Usage: watch.sh [options] [cfg file] [app and command....] |
3 | 3 | # |
| 4 | +# Options: |
| 5 | +# --tee-out-to FILE Tee stdout to FILE |
| 6 | +# --tee-err-to FILE Tee stderr to FILE |
| 7 | +# |
| 8 | + |
| 9 | +# Kill a process, if it is still running. |
| 10 | +# Use: |
| 11 | +# silent_kill $PID |
| 12 | +silent_kill() { |
| 13 | + local pid=$1 |
| 14 | + if kill -0 "$pid" 2>/dev/null && ! kill "$pid" 2>/dev/null; then |
| 15 | + echo "[WARN] Failed to kill process $pid" >&2 |
| 16 | + fi |
| 17 | +} |
4 | 18 |
|
5 | 19 | quitter() { |
6 | | - kill "$PID" |
7 | | - exit |
| 20 | + silent_kill "$PID" |
| 21 | + exit |
8 | 22 | } |
9 | 23 | trap quitter SIGINT |
10 | 24 |
|
| 25 | +# Parse optional tee arguments |
| 26 | +tee_stdout="" |
| 27 | +tee_stderr="" |
| 28 | + |
| 29 | +while [[ $# -gt 0 ]]; do |
| 30 | + case $1 in |
| 31 | + --tee-out-to) |
| 32 | + if [[ -z "$2" || "$2" == --* ]]; then |
| 33 | + echo "Error: --tee-out-to requires a file path argument." >&2 |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + tee_stdout="$2" |
| 37 | + shift 2 |
| 38 | + ;; |
| 39 | + --tee-err-to) |
| 40 | + if [[ -z "$2" || "$2" == --* ]]; then |
| 41 | + echo "Error: --tee-err-to requires a file path argument." >&2 |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + tee_stderr="$2" |
| 45 | + shift 2 |
| 46 | + ;; |
| 47 | + *) |
| 48 | + break |
| 49 | + ;; |
| 50 | + esac |
| 51 | +done |
| 52 | + |
11 | 53 | file_to_watch="$1" |
12 | 54 | shift |
13 | 55 |
|
14 | 56 | wait_for_change_to() { |
15 | | - if which inotifywait; then |
16 | | - echo "[INFO] inotifywaiting to [${file_to_watch}]" |
17 | | - inotifywait -e modify -e move -e create -e delete -e attrib -r "${file_to_watch}" |
18 | | - else |
19 | | - m=$(date -r "${file_to_watch}" +%s) |
20 | | - echo "[INFO] stat checking [${file_to_watch}] from [${m}]" |
21 | | - while true; do |
22 | | - sleep 1 |
23 | | - n=$(date -r "${file_to_watch}" +%s) |
24 | | - echo "[INFO] stat checking [${file_to_watch}] from [${m} < ${n}]" |
25 | | - if [[ $m < $n ]]; then |
26 | | - return |
27 | | - fi |
28 | | - done |
29 | | - fi |
| 57 | + if which inotifywait; then |
| 58 | + echo "[INFO] inotifywaiting to [${file_to_watch}]" |
| 59 | + inotifywait -e modify -e move -e create -e delete -e attrib -r "${file_to_watch}" |
| 60 | + else |
| 61 | + m=$(date -r "${file_to_watch}" +%s) |
| 62 | + echo "[INFO] stat checking [${file_to_watch}] from [${m}]" |
| 63 | + while true; do |
| 64 | + sleep 1 |
| 65 | + n=$(date -r "${file_to_watch}" +%s) |
| 66 | + echo "[INFO] stat checking [${file_to_watch}] from [${m} < ${n}]" |
| 67 | + if [[ $m < $n ]]; then |
| 68 | + return |
| 69 | + fi |
| 70 | + done |
| 71 | + fi |
30 | 72 | } |
31 | 73 |
|
32 | 74 | while true; do |
33 | | - "$@" & |
34 | | - PID=$! |
35 | | - wait_for_change_to "${file_to_watch}" |
36 | | - kill $PID |
37 | | - echo "[INFO] restarting [${PID}] due to modified file" |
| 75 | + # Build command with optional tee for stdout/stderr |
| 76 | + if [[ -n "$tee_stdout" ]] || [[ -n "$tee_stderr" ]]; then |
| 77 | + # Ensure log directories exist |
| 78 | + [[ -n "$tee_stdout" ]] && mkdir -p "$(dirname "$tee_stdout")" |
| 79 | + [[ -n "$tee_stderr" ]] && mkdir -p "$(dirname "$tee_stderr")" |
| 80 | + |
| 81 | + # Run command with tee for stdout and/or stderr |
| 82 | + if [[ -n "$tee_stdout" ]] && [[ -n "$tee_stderr" ]]; then |
| 83 | + # Both stdout and stderr tee |
| 84 | + "$@" > >(tee -a "$tee_stdout") 2> >(tee -a "$tee_stderr" >&2) & |
| 85 | + elif [[ -n "$tee_stdout" ]]; then |
| 86 | + # Only stdout tee |
| 87 | + "$@" > >(tee -a "$tee_stdout") & |
| 88 | + else |
| 89 | + # Only stderr tee |
| 90 | + "$@" 2> >(tee -a "$tee_stderr" >&2) & |
| 91 | + fi |
| 92 | + else |
| 93 | + # No tee, run normally |
| 94 | + "$@" & |
| 95 | + fi |
| 96 | + |
| 97 | + PID=$! |
| 98 | + wait_for_change_to "${file_to_watch}" |
| 99 | + silent_kill "$PID" |
| 100 | + echo "[INFO] restarting [${PID}] due to modified file" |
38 | 101 | done |
0 commit comments