Skip to content
mentoor

Topic 5 of 5 · 5 min read

Sharing machines and processes safely with other agents

Goal: Spot the command in an agent's log that could kill or starve another agent's processes and replace it with a scoped one.

On a machine other people and agents also use, touch only what you started: record each process id when you launch it, stop by that id, and run heavy jobs through a capped, one-at-a-time wrapper. Matching processes by name is how one agent takes down another's work.

Check yourself · 1 of 3

On a shared server, which command is safest for stopping the dev server you started?

Choose one answer

Why it matters

Agents run commands without seeing the whole machine. pkill node stops every Node process the user can signal, including a teammate's dev server or a production app. A full typecheck with no memory cap can use tens of gigabytes and push other jobs out of memory. The agent did what it was told; the harm came from not scoping the command.

How to do it

  1. Record the process id. Start background work and save its id: npm run dev & echo $! > /tmp/mine.pid.
  2. Stop by id, after checking. ps -o args= -p "$(cat /tmp/mine.pid)" shows the command line. If it is yours, kill "$(cat /tmp/mine.pid)". Use a process group (kill -- -<pgid>) when the job spawned children.
  3. Never stop by name, port or pattern. Avoid pkill, killall, fuser -k and pgrep -f ... | xargs kill. The name matches processes you did not start.
  4. Cap heavy jobs. Typechecks, builds, test suites and encodes go through a wrapper that sets a memory limit and CPU share, such as a cgroup or systemd-run --scope -p MemoryMax=8G.
  5. Take turns. Allow one heavy command at a time machine-wide, using a lock or a queue, and wait instead of starting a second one.
  6. Handle lock files with care. A lock file names an owner. Before removing one, check that the process that wrote it is dead. Never delete it just because it blocks you.
  7. Clean up. When the job ends, confirm none of its processes are left running.

Before and after

Weak, from an agent's log:

pkill -f "node"      # free up the port
npx tsc --noEmit     # typecheck

Better:

kill "$(cat /tmp/mine.pid)"          # after checking ps -o args= -p
heavy-run npx tsc --noEmit           # capped, one at a time

What goes wrong

  • Name-based kills. They hit other tenants' processes with the same name.
  • Uncapped heavy jobs. One typecheck can exhaust memory for the whole machine.
  • Parallel heavy jobs. Each is fine alone; together they starve each other.
  • Deleting a lock file. If its owner is alive, two processes now write to the same state.
  • Leftover daemons. A build tool daemon outlives the job and holds memory and a slot.

Examples

The log that should stop you

$ lsof -ti :3000 | xargs kill -9
$ pkill -f vite

The port may belong to another user's app, and vite matches every dev server on the machine. Replace both with the id saved when you started your own server.

A scoped start and stop

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

The id file is the proof that the process is yours.

Key points

  • Record the id of every process you start and stop only those ids.
  • Never kill by name, port or pattern; pkill, killall and fuser -k hit processes that are not yours.
  • Run heavy jobs through a wrapper with a memory and CPU cap, one at a time.
  • A lock file belongs to a process; check that its owner is dead before removing it.
  • When a job ends, confirm that none of its processes or daemons are still running.

Common mistakes

  • Using pkill or killall because the name looks unique.
  • Freeing a port by killing whatever listens on it.
  • Starting a second heavy job while the first is still running.
  • Deleting a lock file without checking that its owner is gone.

Check yourself · 2 of 3

Three agents each start a full typecheck at the same time on one server, and memory runs out. What is the right rule?

Choose one answer

Check yourself · 3 of 3

A build fails with a message that a lock file already exists. What should an agent do before removing it?

Choose one answer

Practice items, not counted towards your mastery.

Start here