如何通过NodeJS从PID/Port获取进程信息并过滤Node服务
First off, the port/PID list you provided doesn’t directly flag which processes are Node.js servers—since it only shows PIDs, we need to map those PIDs to their actual process names. Here’s how to narrow down your target processes and dig up more details about them:
1. Filter Node.js Processes from Your Existing List
For each PID in your list, run the OS-specific command below to check if it’s a Node.js process:
Windows
Check a single PID (e.g., PID 19908):
wmic process where processid=19908 get name,executablepathIf the result shows
node.exeas the name, that’s a Node.js server.Batch-check all PIDs (save your PIDs to a text file like
pids.txtfirst):for /f %p in (pids.txt) do wmic process where processid=%p get name,processid
macOS/Linux
Check a single PID (e.g., PID 19908):
ps -p 19908 -o comm=If the output is
node, that’s your target process.Batch-check multiple PIDs:
for pid in $(cat pids.txt); do echo "$pid: $(ps -p $pid -o comm=)"; done
Once you spot the Node.js PIDs, cross-reference them back to your original port list to get their corresponding ports (like 3000, 3100, etc.).
2. A Smarter Approach: Directly List Node.js Processes & Ports
Instead of filtering from your existing list, use commands that directly show Node.js processes alongside their listening ports:
Windows
for /f "tokens=2,5" %a in ('netstat -ano ^| findstr LISTENING') do @if "%b" neq "" (tasklist /fi "PID eq %b" /fo csv | findstr /i node >nul && echo Port: %a, PID: %b)
This will output only Node.js processes with their associated listening ports.
macOS/Linux
lsof -i -P | grep -i node | grep LISTEN
For Linux users, this works too:
netstat -tulpn | grep node
These commands give you the port, PID, and process name all in one go.
3. Get Detailed Process Info via PID/Port
Once you have a PID or port, here’s how to pull up full details about the process:
By PID
Windows
Get comprehensive details including command line and executable path:
wmic process where processid=19908 get name,executablepath,commandline,creationdate /format:list
Or a simpler, more readable view:
tasklist /v /fi "PID eq 19908"
macOS/Linux
View the full command line used to start the process:
ps -p 19908 -f
On Linux, you can also read process details directly from the /proc filesystem:
cat /proc/19908/cmdline | tr '\0' ' '
By Port
First, get the PID linked to the port, then use the PID-based commands above:
Windows
netstat -ano | findstr :3000
Take the PID from the output, then run your preferred PID details command.
macOS/Linux
lsof -i :3000
Or for Linux:
netstat -tulpn | grep :3000
Use the resulting PID to fetch full process info.
内容的提问来源于stack exchange,提问作者kuldeep singh

