File size: 5,948 Bytes
020c337 | 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | #!/usr/bin/env bash
set -euo pipefail
MANAGER_PID_FILE="/var/run/openclaw/manager.pid"
LOG_FILE="${OPENCLAW_GATEWAY_CTL_LOG_FILE:-/var/log/openclaw/gateway-ctl.log}"
ts() { date -u +"%Y-%m-%dT%H:%M:%SZ"; }
log_info() {
local msg="[$(ts)] [INFO] openclaw-gateway-ctl: $*"
printf '%s\n' "$msg"
mkdir -p "$(dirname "$LOG_FILE")"
printf '%s\n' "$msg" >> "$LOG_FILE"
}
log_error() {
local msg="[$(ts)] [ERROR] openclaw-gateway-ctl: $*"
printf '%s\n' "$msg" >&2
mkdir -p "$(dirname "$LOG_FILE")"
printf '%s\n' "$msg" >> "$LOG_FILE"
}
get_manager_pid() {
if [[ -f "$MANAGER_PID_FILE" ]]; then
local pid
pid=$(cat "$MANAGER_PID_FILE" 2>/dev/null | tr -d '[:space:]')
if [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null; then
echo "$pid"
return 0
fi
fi
return 1
}
get_gateway_pid() {
local port="${OPENCLAW_GATEWAY_PORT:-18789}"
local pid
pid=$(lsof -ti :"$port" -sTCP:LISTEN 2>/dev/null | head -1 || true)
if [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null; then
local comm
comm=$(ps -p "$pid" -o comm= 2>/dev/null || true)
if [[ "$comm" == *"openclaw"* ]]; then
echo "$pid"
return 0
fi
fi
return 1
}
do_status() {
local manager_pid gateway_pid
manager_pid=$(get_manager_pid 2>/dev/null) || true
gateway_pid=$(get_gateway_pid 2>/dev/null) || true
if [[ -n "$manager_pid" ]]; then
log_info "manager is running (PID: $manager_pid)"
else
log_info "manager is not running"
fi
if [[ -n "$gateway_pid" ]]; then
log_info "gateway is running (PID: $gateway_pid)"
else
log_info "gateway is not running"
fi
if [[ -n "$manager_pid" && -n "$gateway_pid" ]]; then
return 0
else
return 1
fi
}
do_start() {
local manager_pid
manager_pid=$(get_manager_pid) || true
if [[ -z "$manager_pid" ]]; then
log_error "manager is not running, cannot start gateway"
return 1
fi
log_info "Sending SIGUSR1 to manager (PID: $manager_pid) to start gateway..."
kill -USR1 "$manager_pid" 2>/dev/null || true
local waited=0
local max_wait=60
while [[ $waited -lt $max_wait ]]; do
sleep 2
waited=$((waited + 2))
local gateway_pid
gateway_pid=$(get_gateway_pid 2>/dev/null) || true
if [[ -n "$gateway_pid" ]]; then
log_info "Gateway started successfully (PID: $gateway_pid, waited ${waited}s)"
return 0
fi
done
log_error "Gateway failed to start after ${max_wait}s"
if [[ -f /var/log/openclaw/gateway.stderr.log ]]; then
log_error "Last 5 lines from gateway stderr:"
tail -5 /var/log/openclaw/gateway.stderr.log | while IFS= read -r line; do
log_error " $line"
done
fi
return 1
}
do_stop() {
local gateway_pid
gateway_pid=$(get_gateway_pid) || true
if [[ -z "$gateway_pid" ]]; then
log_error "gateway is not running"
return 1
fi
log_info "Stopping gateway process (PID: $gateway_pid)..."
kill -TERM "$gateway_pid" 2>/dev/null || true
log_info "Waiting for gateway to stop (max 30s)..."
local waited=0
while [[ $waited -lt 30 ]]; do
if ! kill -0 "$gateway_pid" 2>/dev/null; then
log_info "Gateway stopped gracefully"
return 0
fi
sleep 1
waited=$((waited + 1))
done
log_error "Gateway did not stop gracefully, force killing..."
kill -9 "$gateway_pid" 2>/dev/null || true
log_info "Gateway killed"
}
do_restart() {
local manager_pid gateway_pid
manager_pid=$(get_manager_pid) || true
gateway_pid=$(get_gateway_pid) || true
if [[ -z "$manager_pid" ]]; then
log_error "manager is not running, cannot restart gateway"
return 1
fi
if [[ -n "$gateway_pid" ]]; then
log_info "Stopping gateway process (PID: $gateway_pid)..."
kill -TERM "$gateway_pid" 2>/dev/null || true
sleep 1
fi
log_info "Sending SIGUSR1 to manager (PID: $manager_pid) to restart gateway..."
kill -USR1 "$manager_pid" 2>/dev/null || true
local waited=0
local max_wait=60
while [[ $waited -lt $max_wait ]]; do
sleep 2
waited=$((waited + 2))
gateway_pid=$(get_gateway_pid 2>/dev/null) || true
if [[ -n "$gateway_pid" ]]; then
log_info "Gateway restarted successfully (PID: $gateway_pid, waited ${waited}s)"
return 0
fi
done
log_error "Gateway failed to restart after ${max_wait}s"
if [[ -f /var/log/openclaw/gateway.stderr.log ]]; then
log_error "Last 5 lines from gateway stderr:"
tail -5 /var/log/openclaw/gateway.stderr.log | while IFS= read -r line; do
log_error " $line"
done
fi
return 1
}
do_reload() {
local gateway_pid
gateway_pid=$(get_gateway_pid) || true
if [[ -z "$gateway_pid" ]]; then
log_error "gateway is not running"
return 1
fi
log_info "Sending SIGHUP to gateway (PID: $gateway_pid)..."
kill -HUP "$gateway_pid" 2>/dev/null || true
}
ACTION="${1:-}"
case "$ACTION" in
start|START)
do_start
;;
stop|STOP)
do_stop
;;
restart|RESTART)
do_restart
;;
reload|RELOAD)
do_reload
;;
status|STATUS|"")
do_status
;;
*)
echo "Usage: openclaw-gateway-ctl {start|stop|restart|reload|status}"
echo ""
echo " start - Start gateway (via manager)"
echo " stop - Stop only gateway process (manager keeps running)"
echo " restart - Stop gateway then start via manager (no bootstrap)"
echo " reload - Send SIGHUP to gateway for config reload"
echo " status - Show running status"
exit 1
;;
esac
|