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

如何通过PID在SSH中查看后台任务?浏览器启动进程ps查询无结果求助

Hey there, let’s tackle this problem head-on—this is a super common issue when dealing with browser-spawned background processes and long-running scripts that hang. I’ve got a bunch of practical tools and explanations to help you diagnose what’s going on.

1. Why ps -p <pid> Might Not Show Your Process

First, let’s figure out why your target PID isn’t showing up with basic ps commands:

  • The process may have already exited: Even if your script "hangs," sometimes the parent browser-spawned process dies unexpectedly. Test if the PID is still active with kill -0 <pid>—this command doesn’t kill the process, it just checks if it exists (no output means it’s alive; a "No such process" error means it’s gone).
  • It’s a child process: Browser-launched tasks often spawn child processes to handle sub-tasks. Use pstree -p <pid> to visualize the entire process tree rooted at your target PID. This will reveal any child processes that might still be running even if the parent exited.
  • Session/TTY mismatch: If the process was started in a detached session (like via a browser that’s not tied to your SSH shell), ps -p won’t surface it by default. Swap to ps -ef | grep <pid> instead—it lists all system processes, regardless of which session they’re in. Add grep -v grep to filter out the grep command itself from the results.
2. Tracking Internal Processes of Your Long-Running Script

To see exactly what your script is doing while it runs (and where it might be hanging), use these tools:

  • strace for deep system call tracing: Run strace -p <pid> to watch every system call the process makes—file accesses, network requests, signal handling. If it’s stuck, this will show you exactly where (e.g., waiting on a file lock or a slow network response). Add -tt for timestamps, or -e trace=open,read,write to filter only relevant calls.
  • htop for interactive monitoring: Launch htop and search for your PID with /<pid>. It’ll show you the process’s CPU/memory usage, threads, parent/child relationships, and even let you send signals (like SIGKILL or SIGSTOP) directly from the interface. Way more intuitive than plain ps.
  • lsof to check open resources: Run lsof -p <pid> to list every file, socket, pipe, or device the process has open. If your script is hanging on a file read/write or a network connection, this will point you straight to the problematic resource.
  • Add logging to your script: For future runs, modify your script to log each task’s start/end time and status to a file. This makes it way easier to pinpoint which step is causing hangs without needing real-time tools.
3. Viewing Background Tasks in SSH Using PID

If you need to check on a background task via SSH using its PID, here are your best bets:

  • ps aux | grep <pid>: This is the go-to for getting full context about the process—you’ll see the owner, CPU/memory usage, full command line, and which TTY/session it’s tied to. Again, add grep -v grep to clean up the output.
  • ps -o pid,ppid,cmd -p <pid>: A more concise view that shows just the PID, parent PID, and the command that started the process. Great for quick checks.
  • Check session-aware processes with ps -ejH: This groups processes by their session ID, which is helpful if your task is running in a detached session (like via nohup or screen). You can cross-reference the session ID with your PID to see its full context.
  • jobs -l (for current shell sessions only): If you started the process in your current SSH session with &, jobs -l will list all background jobs along with their PIDs. Note that this only works for jobs tied to your active shell—if you disconnected and reconnected, they won’t show up here (use ps instead for those cases).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:25:06