Skip to content

Commit 5ca7ca4

Browse files
RahulHereRahulHere
authored andcommitted
Fix example script cleanup to terminate all child processes
The npm run dev command spawns child processes that weren't being terminated when the parent PID was killed. This fix ensures proper cleanup by using process groups and port-based killing as fallback. Changes: - Kill by process group (negative PID) to terminate child processes - Add port-based killing via lsof as fallback for remaining processes - Add INT and TERM signals to the cleanup trap
1 parent 82c4cf7 commit 5ca7ca4

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

examples/client_example_json_run.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ NC='\033[0m' # No Color
1414
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1515
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
1616

17-
# Cleanup function
17+
# Cleanup function - kill process groups to ensure all child processes are terminated
1818
cleanup() {
1919
echo -e "\n${YELLOW}Cleaning up...${NC}"
20+
# Kill by process group (negative PID)
2021
if [ -n "$SERVER3001_PID" ]; then
21-
kill $SERVER3001_PID 2>/dev/null || true
22+
kill -- -$SERVER3001_PID 2>/dev/null || kill $SERVER3001_PID 2>/dev/null || true
2223
fi
2324
if [ -n "$SERVER3002_PID" ]; then
24-
kill $SERVER3002_PID 2>/dev/null || true
25+
kill -- -$SERVER3002_PID 2>/dev/null || kill $SERVER3002_PID 2>/dev/null || true
2526
fi
27+
# Also kill any remaining node processes on the specific ports
28+
lsof -ti:3001 | xargs kill 2>/dev/null || true
29+
lsof -ti:3002 | xargs kill 2>/dev/null || true
2630
echo -e "${GREEN}Done${NC}"
2731
}
2832

29-
trap cleanup EXIT
33+
trap cleanup EXIT INT TERM
3034

3135
echo -e "${GREEN}======================================${NC}"
3236
echo -e "${GREEN}Running PHP Client Example${NC}"

0 commit comments

Comments
 (0)