Why Killing a Process Doesn’t Stop the Job It Belongs To

Date:

You kill the worker, watch it exit, and move on. Minutes later the job you thought you had stopped is still doing work, or has quietly finished on its own.

The short answer: When a shell script launches a worker and you kill only the worker, the script does not stop. It returns from that call and runs its next line, and if that line starts another job, you now have an unattended process doing work you thought you had cancelled. In one case, a wrapper ran two batches back to back; the first batch’s worker was killed by name and seen to exit, and the wrapper advanced to the second batch and ran it for 25 minutes, unwatched. The fix is to act on the job, not the process: signal the process group, not the leaf, and have the wrapper write its PID so “stop” has one target. Killing a process ends a process; ending a job means killing whatever starts the next one.

An orphaned child process is a worker whose parent keeps running after the worker is killed, so the parent’s remaining work proceeds as if nothing was cancelled.

How a stopped job keeps running

The setup that produces this is ordinary: a small shell script that runs two jobs in sequence, first one batch, then another. Something looks wrong partway through the first batch, so you stop it the way reflex says to: find the worker, kill it, done. The worker dies, you confirm it is gone, and you turn to something else.

What is easy to miss is that the worker was a child of the shell script, and the shell script is still alive. From its point of view the child has simply finished; that is all a killed subprocess looks like to its parent. So the script does what it was written to do: it moves to its next line and launches the second batch.

In the case above, that second batch ran for 25 minutes and completed about two dozen units of work before reaching its own natural end. Nobody noticed until they went looking for the half-finished job and found it done.

A killed subprocess and a finished subprocess look identical to the parent that launched it.

Why it stays invisible

Three things conspire to hide it, and each is worth naming because they recur.

First, the kill works. There is no error, no zombie, no warning: the thing you aimed at dies exactly as asked. Success on the wrong target feels the same as success.

Second, the parent is silent. A wrapper script that just calls two commands prints nothing of its own between them. There is no “starting batch two” line to tell you the sequence is still marching forward.

Third, you killed by name, not by lineage. Reaching for the worker by what it was (its program name) rather than by where it sat in the process tree means you never consider that something launched it and was waiting on it.

The uncomfortable part is that none of this is exotic. It is the default behavior of every shell that runs one command after another.

Success on the wrong target feels exactly like success on the right one.

Proving it was the wrapper, not a restart

The tempting first explanation is the wrong one: that something restarted the job, such as a scheduler, a retry or a watcher. That would have been a different and worse bug.

The logs rule it out cleanly. The wrapper’s own output showed the two batches as one continuous run: a header for the first job, then (with no gap, no second invocation, no restart banner) a header for the second, and a completion line 25 minutes later. One process, two phases, exactly as written. The second batch had not been relaunched; it had never been prevented from starting.

That distinction matters for the fix. A restart bug is fixed by finding what restarts. A run-on bug is fixed by killing the right thing in the first place.

The fix, cheapest first

Kill the job, not the leaf. The moment you have a wrapper, “stop this” means stop the wrapper, or the whole process group, not the one worker you can see. On a POSIX shell, launching the job in its own process group and signalling that group takes the parent and every child with it. The worker-by-name habit is the trap.

Make the wrapper announce its PID. The cheapest durable defense costs one line at the top of the script: write its own process id to a known file. Now “stop the job” has a single unambiguous target, and stopping it means killing the thing that starts the next phase, not the current one.

#!/usr/bin/env bash
echo $$ > run.pid      # the job identity, not the worker
run_first_batch
run_second_batch
rm -f run.pidCode language: PHP (php)

Give the parent a voice. A one-line “starting phase two” between jobs turns an invisible run-on into something you would have caught in the first ten seconds. Silence is what let 25 minutes pass.

When in doubt, check the tree, not the list. Before concluding a job is stopped, look at what is still running and what launched it. A live parent with a dead child is the signature of exactly this mistake.

Stop the thing that starts the next job, or the next job starts without you.

What the fix cannot do

A PID file and a process group make the job killable; they do not make it safe to kill at any moment. If a worker is halfway through something that must complete as a unit, killing its group mid-step can leave that unit half-done. That calls for a separate discipline: make each unit of work resumable, so an interrupted job costs one item, not a corrupt state, the same discipline that lets you reconcile before you recreate on the retry. The process-group fix ensures you can stop the whole job; idempotent, resumable work is what makes stopping it cheap. You want both, and they solve different halves of the problem. It is the same process-hygiene instinct behind giving each parallel agent its own git worktree so concurrent jobs never collide.

The short version

A shell wrapper ran two batches in sequence. Killing the first batch’s worker by process name stopped that worker but not the wrapper, which returned from the call, advanced to its next line, and ran the second batch unattended for 25 minutes. The logs proved it was a single continuous run, not a restart. The lesson is to operate on the job rather than the process: launch it in its own process group and signal the group, write the wrapper’s PID to a file so “stop” has one target, and give the parent a line of output between phases so a run-on is visible. A PID file makes the job killable; resumable work is what makes killing it cheap.

Frequently Asked Questions

How do I kill an entire job and all its subprocesses at once?

Launch the job in its own process group and send the signal to the group rather than to a single PID. On a POSIX shell that means signalling the negative of the group id, which reaches the leader and every descendant. Killing by program name reaches only the processes that happen to share that name, which is rarely the whole job.

Why does Ctrl+C stop the whole job when killing the worker doesn’t?

Because Ctrl+C in a terminal sends the interrupt to the entire foreground process group, so the wrapper and its worker get it together. The trap appears when you stop the job from somewhere else, such as another terminal or a task manager, and pick the worker by name. That signal reaches only the worker, and the wrapper carries on.

Is this only a shell scripting problem?

No. Any parent-child arrangement has it: a process manager and its workers, a test runner spawning subprocesses, a CI step that backgrounds a task. Whenever one thing launches another and then continues, killing the launched thing leaves the launcher free to proceed. The shell wrapper is just the most common place to meet it.

Should the parent process trap signals to clean up?

It helps. A parent that traps termination signals can remove its PID file, stop in-flight children, and refuse to start the next phase, turning an abrupt kill into an orderly stop. It is not a substitute for targeting the right process, but it makes the right target behave well when you hit it.

Share post:

Subscribe

Popular

More like this
Related

WooCommerce Search by SKU or ISBN Finds Nothing? It Never Looks There

A shopper types the SKU from your catalog, or the ISBN printed on the back of a book, and gets "No products were found". The product is right there, and the number is on its page. Search simply never reads that part of the product.

Computer Slow but CPU and Memory Look Normal? Check the Handle Count

Chrome takes a minute to open, or doesn't open at all. Every click waits. You open Task Manager expecting to find the culprit, and it tells you everything is fine. Here is the number that isn't, and the fix that takes two minutes.

Kernel-Power 41 Tells You Your PC Restarted, Not Why

Your PC restarts with no blue screen, and the only clue is a critical event saying it didn't shut down cleanly. Here is how to get from that event to a short list of suspects.

Set Up WordPress SMTP Without a Plugin, Even on a Multisite Network

WordPress mail goes out as wordpress@ your domain, from the web server, and lands in spam. An SMTP plugin fixes that. So do about twenty lines of code, and on a network of sites the code can give every site its own mailbox and its own password.