-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht3-stop.sh
More file actions
executable file
·107 lines (102 loc) · 3.86 KB
/
Copy patht3-stop.sh
File metadata and controls
executable file
·107 lines (102 loc) · 3.86 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
# Stops the T3 server CodeLaunch started, and the Cloudflare tunnel it started
# with it. Idempotent: a re-run is a clean no-op.
#
# Only processes recorded as CodeLaunch-owned are signalled. A T3 Desktop app or
# a server you started yourself is reported and left running.
#
# T3 account authentication and the T3 Connect link are never touched - stopping
# the server is not a reason to unlink this environment.
set -euo pipefail
umask 077
cd "$(dirname "$0")"
. ./scripts/env.sh
usage() {
cat <<'EOF'
Usage: ./t3-stop.sh
./t3-stop.sh -h|--help
EOF
}
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help) usage; exit 0 ;;
*) echo "unexpected argument: $1" >&2; usage >&2; exit 2 ;;
esac
done
# --- a. the server we own --------------------------------------------------
# Shutdown reads the recorded mode rather than .env, so a stack started in one
# mode still tears down correctly after the configuration moved on. Called
# directly, not through $( ): the record only reaches this shell that way.
owned_pid=''
owned_children=''
run_mode=''
if codelaunch_t3_owned_pid >/dev/null; then
owned_pid=$CODELAUNCH_T3_PID
run_mode=$CODELAUNCH_T3_MODE
owned_children=$(codelaunch_t3_server_child_pids "$owned_pid" | tr '\n' ' ')
echo "stopping CodeLaunch-owned T3 server (PID $owned_pid, mode $run_mode) ..."
if codelaunch_t3_stop_pid "$owned_pid"; then
if codelaunch_t3_clear_state; then
echo "T3 server stopped"
else
echo "WARNING: T3 server stopped, but its ownership record could not be cleared"
fi
else
echo "WARNING: T3 server is still running after SIGKILL (PID $owned_pid); ownership retained for a later retry"
fi
else
owned_status=$?
if [ "$owned_status" -eq 2 ]; then
echo "WARNING: invalid T3 ownership record; refusing to signal anything."
echo " Inspect and remove $HOME/.codelaunch/run/t3-serve.pid, then rerun ./t3-stop.sh."
else
# codelaunch_t3_owned_pid drops a record it can prove is dead or recycled.
echo "no CodeLaunch-owned T3 server running"
fi
fi
# --- b. the tunnel we started ----------------------------------------------
tunnel_name=''
if codelaunch_t3_read_tunnel_state; then
tunnel_name=$CODELAUNCH_T3_TUNNEL
else
tunnel_status=$?
if [ "$tunnel_status" -eq 2 ]; then
echo "WARNING: invalid tunnel ownership record; leaving cloudflared alone."
echo " Inspect and remove $HOME/.codelaunch/run/t3-tunnel, then rerun ./t3-stop.sh."
fi
fi
if [ -n "$tunnel_name" ]; then
tunnel_pids=$(codelaunch_tunnel_pids "$tunnel_name")
if [ -n "$tunnel_pids" ]; then
echo "stopping tunnel $tunnel_name ..."
for p in $tunnel_pids; do kill "$p" 2>/dev/null || true; done
for _ in $(seq 1 35); do
tunnel_alive=0
for p in $tunnel_pids; do
if kill -0 "$p" 2>/dev/null; then tunnel_alive=1; break; fi
done
[ "$tunnel_alive" = 0 ] && break
sleep 1
done
tunnel_pids=$(codelaunch_tunnel_pids "$tunnel_name")
if [ -n "$tunnel_pids" ]; then
echo "tunnel still up after grace - sending second signal"
for p in $tunnel_pids; do kill "$p" 2>/dev/null || true; done
fi
fi
if [ -n "$(codelaunch_tunnel_pids "$tunnel_name")" ]; then
echo "WARNING: tunnel $tunnel_name is still running; ownership retained for a later retry"
else
codelaunch_t3_clear_tunnel_state || echo "WARNING: tunnel ownership record could not be cleared"
echo "tunnel $tunnel_name stopped"
fi
fi
# --- c. what was left alone ------------------------------------------------
# Unquoted on purpose - both are numeric PID lists that must word-split.
unowned=$(codelaunch_t3_unowned_pids $owned_pid $owned_children | tr '\n' ' ')
unowned=${unowned% }
if [ -n "$unowned" ]; then
echo "headless T3 server(s) not started by CodeLaunch left running (PID $unowned)"
echo " Stop one yourself with: kill <pid>"
fi
echo "T3 sign-in and the T3 Connect link are untouched"