How to Extract Specific PID from lsof Output for Port 8081 in React Native Development


4 views

When working with React Native development, you often need to manage the Metro bundler running on port 8081. A common task is to kill the process using this port before starting a new instance. The standard approach involves using lsof to find the process ID (PID) and then terminating it with kill -9.

The raw lsof -i :8081 command returns multiple entries, including browser connections and other services. We need to specifically target the Node.js process running the Metro bundler. Here's what a typical output looks like:

COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
node      16210 loow   16u  IPv6 13747716      0t0  TCP *:tproxy (LISTEN)

To extract just the Node process PID, we can combine several Unix tools:

lsof -i :8081 | grep 'node' | grep 'LISTEN' | awk '{print $2}'

This pipeline:

  1. Lists all processes using port 8081
  2. Filters for lines containing "node"
  3. Further filters for the listening process
  4. Extracts just the PID column

Here's a complete bash script example that safely kills the Metro bundler:

#!/bin/bash

PORT=8081
PID=$(lsof -i :${PORT} | grep 'node' | grep 'LISTEN' | awk '{print $2}')

if [ -n "${PID}" ]; then
    echo "Killing process ${PID} using port ${PORT}"
    kill -9 ${PID}
else
    echo "No process found using port ${PORT}"
fi

For more reliability across different systems, consider these variations:

# Using pgrep (simpler but less precise)
pgrep -f "react-native start" | xargs kill -9

# Using netstat (for systems without lsof)
netstat -tulpn | grep :8081 | awk '{print $7}' | cut -d'/' -f1 | xargs kill -9

Be aware of these potential issues:

  • Multiple Node processes might appear in the output
  • On some systems, the port might be listed as "tproxy" instead of the actual number
  • The user running the command might not have permission to kill the process

When working with React Native development, you often need to kill processes occupying port 8081 before restarting your development server. The standard approach using lsof -i :8081 might return multiple process entries, making PID extraction challenging.

The raw output typically shows multiple processes:

COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
node      16210 loow   16u  IPv6 13747716      0t0  TCP *:tproxy (LISTEN)
chrome     2423 loow  127u  IPv4 13749099      0t0  TCP localhost.localdomain:36650->localhost.localdomain:tproxy (ESTABLISHED)

Here are three effective methods to get just the Node.js process PID:

Method 1: Using lsof with awk

lsof -ti :8081 | xargs -I {} ps -p {} -o comm= | grep -n node | cut -d: -f1 | xargs -I {} lsof -t :8081 | sed -n {}p

Method 2: Combining lsof and pgrep

kill -9 $(pgrep -f "node.*8081")

Method 3: Advanced grep filtering

lsof -i :8081 | grep -E 'node.*LISTEN' | awk '{print $2}' | head -n 1

Here's a robust bash script for React Native development:

#!/bin/bash
PORT=8081
PID=$(lsof -i :${PORT} | grep -E 'node.*LISTEN' | awk '{print $2}' | head -n 1)

if [[ -n "$PID" ]]; then
    echo "Killing process $PID occupying port $PORT"
    kill -9 $PID
else
    echo "No process found on port $PORT"
fi

For more complex scenarios:

# Multiple node processes case
PIDS=$(lsof -ti :8081 | xargs -I {} ps -p {} -o comm= | grep -n node | cut -d: -f1 | xargs -I {} lsof -t :8081)
for PID in $PIDS; do
    kill -9 $PID
done

Consider these alternatives to lsof:

  • ss -tulnp | grep 8081
  • netstat -tulnp | grep 8081
  • fuser 8081/tcp