如何通过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 -pwon’t surface it by default. Swap tops -ef | grep <pid>instead—it lists all system processes, regardless of which session they’re in. Addgrep -v grepto 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:
stracefor deep system call tracing: Runstrace -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-ttfor timestamps, or-e trace=open,read,writeto filter only relevant calls.htopfor interactive monitoring: Launchhtopand 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 plainps.lsofto check open resources: Runlsof -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, addgrep -v grepto 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 vianohuporscreen). 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 -lwill 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 (usepsinstead for those cases).
内容的提问来源于stack exchange,提问作者Ravi Prakash
相关产品推荐
相关产品推荐

