#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)"
WORK="$ROOT/outputs/linux/linux-process"
mkdir -p -- "$WORK"
sleep 20 &
    child_pid=$!
    cleanup() {
      if [[ -n "${child_pid-}" ]]; then
        kill "$child_pid" 2>/dev/null || true
        wait "$child_pid" 2>/dev/null || true
      fi
    }
    trap cleanup EXIT
    printf 'started own child pid=%s\n' "$child_pid"
if ps -p "$child_pid" -o pid=,comm= 2>/dev/null; then
  printf '%s\n' 'GNU ps inspection succeeded'
else
  printf '%s\n' '当前 ps 不支持 GNU -o；用 kill -0 验证自建子进程仍存在，Ubuntu ps 输出待验证'
  kill -0 "$child_pid"
fi
    kill "$child_pid"
    if wait "$child_pid"; then
      printf '%s\n' 'child exited normally'
    else
      status=$?
      printf 'own child terminated, wait status=%s\n' "$status"
    fi
    child_pid=''
    printf '%s\n' 'only the child created by this script was stopped' > "$WORK/process.log"
    cat "$WORK/process.log"
