You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node进程在Bash脚本中运行时崩溃,客户端-服务端测试异常

Why Your Node Server Gets Killed in a Bash Script (and How to Fix It)

Hey there, let's break down why your Node server is dying when you launch it via a Bash script, even though manual startup works perfectly. The issue almost always boils down to process group handling or incorrect script logic—here's what's happening and how to fix it:

What's Causing the Problem?

When you start the server with node Server/Server --port 3300 & in your script, it runs as a background process in the same process group as the script. When the script finishes executing your tests and exits, Bash sends a SIGHUP (hangup) signal to all processes in its group. Your Node server receives this signal and terminates automatically.

Also, I noticed a couple of syntax issues in your script snippet:

  • &> is HTML-escaped; you need &> to redirect both stdout and stderr to your temp file.
  • $(basename 0) should be $(basename "$0") to reference the script filename correctly (not the literal character "0").

Fixes to Try

1. Detach the Server from the Script's Process Group

Use disown to remove the server from the script's job list, so it doesn't receive the SIGHUP signal when the script exits:

#!/usr/bin/env bash
output=$(mktemp "${TMPDIR:-/tmp/}$(basename "$0").XXX")
# Start server and redirect output
node Server/Server --port 3300 &> "$output" &
# Detach the process from the script's job control
disown
# Run your tests
npm test # Replace with your actual test command

Alternatively, use nohup to explicitly ignore SIGHUP signals:

#!/usr/bin/env bash
output=$(mktemp "${TMPDIR:-/tmp/}$(basename "$0").XXX")
# nohup ignores SIGHUP and redirects output (we override the default nohup.out)
nohup node Server/Server --port 3300 &> "$output" &
# Run tests
npm test

2. Manually Manage the Server's Lifecycle (Most Reliable)

For test workflows, it's best to start the server, wait for it to be ready, run your tests, then clean up the server explicitly. This avoids orphaned processes and ensures the server only runs while tests are executing:

#!/usr/bin/env bash
output=$(mktemp "${TMPDIR:-/tmp/}$(basename "$0").XXX")
# Start server and save its PID
node Server/Server --port 3300 &> "$output" &
SERVER_PID=$!

# Wait for the server to fully start (check if port 3300 is listening)
until nc -z localhost 3300; do
  sleep 0.5
  echo "Waiting for server to start..."
done

# Run your test suite
npm test

# Clean up: kill the server and remove temp file
kill "$SERVER_PID"
rm "$output"

This approach guarantees the server stays alive only as long as needed, and you avoid unexpected terminations from process group signals.

3. Verify Temp File Permissions

Ensure the temporary file you create has write permissions for the user running the script. The mktemp command usually handles this, but if you're running into issues, you can explicitly set permissions with:

chmod 600 "$output"

Key Takeaways

  • Background processes in a script inherit the script's process group—use disown or nohup to detach them if you want them to survive script exit.
  • For test automation, always manage the server's lifecycle explicitly (start → wait → test → stop) to avoid surprises.
  • Double-check script syntax (especially redirects and variable references) to avoid hidden errors.

内容的提问来源于stack exchange,提问作者Yinon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:22:44