Skip to content
mentoor

Topic 3 of 5 · 4 min read

Processes, signals and background jobs

Goal: Find and stop only the process an agent started, by its process id, without touching other services on the same machine.

Every process has a number, and the safe way to stop one is to use the number you recorded when you started it. Names, ports and patterns match things you do not own.

Check yourself · 1 of 3

In bash, which value holds the process id of the most recent background job you started?

Choose one answer

Why it matters

Agents start dev servers, test watchers and builds, and often leave them running in the background. The next run then fails with "port already in use", and the quick fix the model reaches for is a kill by name. On a machine with other people's apps, that is how an outage starts.

The parts

  • PID: the process id, a number such as 48213. $! holds the PID of the last background job in the shell. ps -o pid,etime,args -p 48213 shows how long it has run and its full command line.
  • Process group (PGID): a set of processes started together, for example a server and its workers. kill -- -48213 signals the whole group.
  • Signals: kill <pid> sends SIGTERM (15), a polite request that lets the program flush files and exit. SIGKILL (kill -9) cannot be caught: the process dies at once, with no cleanup, and may leave lock files or half-written data. Try SIGTERM first, wait, check, and only then escalate. The signal(7) page lists which signals can be caught.
  • Ports: ss -ltnp lists listening sockets and the process that owns each. Look the owner up by PID before assuming it is yours.
  • Background jobs: cmd & runs in the background; jobs lists those of this shell. A job started with nohup survives the shell closing and is not listed by jobs later, so write its PID to a file.

Before and after

Weak:

pkill -f "node server.js"
npm run dev &

Fixed:

npm run dev > /tmp/dev.log 2>&1 &
echo $! > /tmp/dev.pid
# later
ps -o args= -p "$(cat /tmp/dev.pid)"   # confirm it is ours
kill "$(cat /tmp/dev.pid)"

The fixed version records the PID, checks the command line before acting and sends the gentle signal.

What goes wrong

  • Same name, other owner. Several sessions run node or tsc. A pattern kill hits all of them.
  • Killing the wrapper only. npm exits but the child it started keeps the port. Stop the group or the child PID.
  • Reaching for -9 first. A database or build tool is killed mid-write and leaves a lock behind.
  • Leaving jobs behind. When the task ends, confirm with ps that nothing you started is still running.

Examples

Port already in use

ss -ltnp | grep ':3000'
# LISTEN 0 511 *:3000 *:* users:(("node",pid=7312,fd=19))
ps -o pid,etime,args -p 7312

The listener has run for three days and its command line points to another project. It is not the agent's server, so do not kill it. Start the agent's server on a different port instead.

Gentle then firm

kill 48213
sleep 5
ps -p 48213 && kill -9 48213

SIGTERM gives the program five seconds to exit cleanly. Only if it is still there does -9 follow.

Key points

  • Record the PID when you start a background process ($! in the shell) and stop that PID later.
  • Check the command line with ps -o args= -p <pid> before sending any signal to be sure it is yours.
  • Send SIGTERM first and escalate to SIGKILL only if the process ignores it, because SIGKILL skips cleanup.
  • Use ss -ltnp to see which process owns a port instead of killing whatever is on it.
  • Stop a process group with kill -- -<pgid> when a command spawned workers, and confirm nothing is left.

Common mistakes

  • Freeing a port by killing whatever listens on it without checking who owns it.
  • Using kill -9 as the first step and leaving lock files or corrupt data behind.
  • Forgetting to save the PID of a background job, then having to guess later which process was yours.
  • Stopping the npm wrapper but not the child process that holds the port.

Check yourself · 2 of 3

What is the main difference between kill <pid> and kill -9 <pid>?

Choose one answer

Check yourself · 3 of 3

Port 8080 is busy. ss shows a node process with a three-day uptime that you did not start. What do you do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic