Find and control a running Node.js process
🎯 Introduction
After starting a service, the terminal may appear stuck. The service is actually still running and waiting for requests. This article uses a Node.js HTTP service to practice starting, inspecting, and stopping processes. You need two terminal windows, both logged in to the same Ubuntu machine.
🧱 Prepare the service and sample data
server.mjs runs on Node.js 24, and notes.txt contains its sample data. Put both in the same directory on Ubuntu so the service can find the data file. First create that directory. server.mjs · notes.txt
mkdir -p "$HOME/linux-lab/app"
Open the two example files linked above and use Vim to save their contents as ~/linux-lab/app/server.mjs and ~/linux-lab/app/notes.txt on Ubuntu.
The program listens for requests on Ubuntu's local port 3000. A port distinguishes network services on one machine. /health checks whether the service is running; /api/notes retrieves the sample data.
🛠 Run the service in the foreground
Start the service in the first Ubuntu window
server.mjs is a file on disk. When node runs it, Linux creates a process and assigns a process ID, or PID. Starting the same file again creates a new process whose PID may differ.
Start the service using the node installed in the previous article.
node "$HOME/linux-lab/app/server.mjs"
A successful startup prints this line.
Node.js HTTP service listening on 127.0.0.1:3000
The prompt for your next command has not returned. The service keeps running in this window, which is called foreground execution. Leave the window open.
Send a request from the second Ubuntu window
Open another terminal and log in to the same Ubuntu machine. 127.0.0.1 refers to the machine running the command, so run curl on Ubuntu. This command requests /health on port 3000.
curl http://127.0.0.1:3000/health
The following response confirms that the service in the first window accepts requests.
{"status":"ok"}
🧰 Find the process you started
Use pgrep in the second window to find the process. -f matches the full command line; -a displays both PID and command. This pattern matches only server.mjs in the course directory. [.] matches the filename's dot literally.
pgrep -af 'linux-lab/app/server[.]mjs'
The output may look like this.
2814 node /home/learner/linux-lab/app/server.mjs
2814 is an example PID; yours will differ. Use the remaining command and path to confirm that this is the service you started.
Once you know the PID, use ps to inspect that process. -p selects the process, and -o pid,args displays only its PID and command line. Replace 2814 with the PID you found.
ps -p 2814 -o pid,args
To watch CPU and memory usage, use top -p 2814, again replacing the PID. Press q to return to the terminal. The display layout may vary by system version.
Return to the first window and press ⌃ Control + C on a Mac keyboard to stop the foreground service. Request /health again from the second window; the connection should fail. This assumes no other program is responding on 3000.
🛠 Run in the background and stop by PID
A foreground process occupies the terminal. Append & to start it in the background and continue entering commands in the same window. Confirm that the earlier process has stopped, then run this in the first Ubuntu window.
node "$HOME/linux-lab/app/server.mjs" &
Bash usually prints something like [1] 2871. [1] is this terminal's background job number; 2871 is the Linux PID. Startup messages may still appear, but you can continue entering commands.
To stop the background process, find its current PID with pgrep, then verify its command with ps. kill sends a signal; TERM requests termination and gives the process a chance to clean up. Replace the example number with the PID you verified.
kill -TERM 2871
Run pgrep again. If the matching process is gone, this run has ended.
❓ Knowledge check
Q1: Will a process started with & keep running after I exit SSH?
It may receive a hangup signal and stop when you exit. For temporary tasks, nohup ignores that signal. Use systemd, covered next, for Node.js services that must run continuously, start at boot, and recover from failures. nohup manual · systemd service management
Q2: How do I choose between kill and pkill?
kill sends a signal to a PID and suits a single verified process. pkill matches names or command lines and may affect several processes. First use pgrep with the same matching criteria to see what would match.
🧪 Exercise
Temporarily run the service on port 3001. The leading PORT=3001 supplies that setting only for this invocation.
PORT=3001 node "$HOME/linux-lab/app/server.mjs"
In another Ubuntu window, run curl http://127.0.0.1:3001/health and expect {"status":"ok"}. Record the PID with pgrep. Return to the startup window, press ⌃ Control + C, and confirm that the service with that PID stopped.
🎉 You can now start a service, find its PID, and stop it after verifying its identity!
