is_running() { local pid=$1 if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then return 0 fi return 1 }
start_daemon() { local name=$1 local cmd=$2 local log_file="$LOG_DIR/$name.log" local pid_file="$PID_DIR/$name.pid"
if [ -f "$pid_file" ]; then old_pid=$(cat"$pid_file") if is_running "$old_pid"; then echo"[!] $name already running (PID $old_pid)" return else echo"[!] removing stale PID file for $name" rm -f "$pid_file" fi fi
echo"[+] starting $name ..."
nohup bash -c " while true; do echo '[*] $(date) starting $name' $cmd echo '[!] $(date)$name crashed, restarting in 2s...' sleep 2 done " > "$log_file" 2>&1 &
stop_daemon() { local name=$1 local pid_file="$PID_DIR/$name.pid"
if [ ! -f "$pid_file" ]; then echo"[!] $name not running (no pid file)" return fi
pid=$(cat"$pid_file")
if is_running "$pid"; then kill"$pid" echo"[+] stopped $name (PID $pid)" else echo"[!] $name already stopped" fi
rm -f "$pid_file" }
status_daemon() { local name=$1 local pid_file="$PID_DIR/$name.pid"
if [ -f "$pid_file" ]; then pid=$(cat"$pid_file") if is_running "$pid"; then echo"[RUNNING] $name (PID $pid)" else echo"[DEAD] $name (stale PID $pid)" fi else echo"[STOPPED] $name" fi }
# ========= 业务控制 =========
start() { echo"========== START =========="
start_daemon "npm_watch""npm run watch" start_daemon "frida""python frida-analykit/main.py bootup-server"