# pkill -f kills my own shell: why, and how to stop it

> pkill -f matches every command line that contains your pattern, including the one running pkill. Ours died with 137 and no log. Match a path instead.

Source: https://mosaicbrowser.com/blog/pkill-killed-its-own-shell

## pkill -f killed the shell that was running it

pkill -f matches every command line that contains your pattern, including the one running pkill. Ours died with 137 and no log. Match a path instead.

![137: A build that never started, reported as a failure, beside a screenshot: the Mosaic sidebar with profiles running, each with its own window](https://mosaicbrowser.com/covers/pkill-killed-its-own-shell.webp)

`pkill -f` can kill your own shell because `-f` matches the pattern against every process's full command line, and the shell running your command carries that command, pattern and all, as its argument. We had a command line containing `pkill -f "electron-vite"` die with exit status 137 and no log. A build that never started was reported as a failed one.

## What happened

Mosaic's development loop restarts the app's dev server often, and the way to stop the old one is to kill `electron-vite`. A one-line command did that, then ran the build. The whole invocation ended with 137. No build output, no error, nothing written anywhere.

137 is 128 plus 9, so something was killed with SIGKILL. The build did not fail; it was never reached. What we recorded at the time is the mechanism, not the full process tree, so we cannot say which process in the chain was holding the string. What we can say is that the command line contained `electron-vite`, and so it matched.

## Why pkill -f kills your own shell

Without `-f`, `pkill` matches a process's name. With it, `pkill` matches the whole argument list, which is what you want for `node` processes that all have the same name. The catch is that any process whose arguments contain your pattern is a target, and a shell started as `bash -c "pkill -f electron-vite; npm run build"` contains it.

`pkill` always leaves itself alone. What it does with the processes above it is where macOS and Linux part ways. We checked both on 3 November 2026.

On this Mac, `man pkill` says that by default "the current pgrep or pkill process and all of its ancestors are excluded", and `-a` includes them. The [procps-ng manual](https://man7.org/linux/man-pages/man1/pgrep.1.html) used on most Linux systems says the running pkill "will never report itself as a match", and offers `-A`, `--ignore-ancestors`, as an option. The two defaults point in opposite directions, so a script that is safe on one can kill its own shell on the other.

We reproduced it on macOS with a marker string, putting the ancestors back with `-a`:

```
$ bash -c "sleep 0.1; pkill -a -f zzmark-q7; echo survived"; echo "exit=$?"
exit=143

```

The shell was killed before it could print anything: 143 is 128 plus 15, the SIGTERM a plain `pkill` sends. Without `-a` the same line printed `survived`. And ancestors are not the only risk. Anything beside your command that carries its text, such as a wrapper or a logger that was handed the command as an argument, matches regardless of either rule.

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: REST API](https://mosaicbrowser.com/features/rest-api)

## The fix: match on something only the target has

Our restart script now matches `electron-vite` and Electron by path:

```
pkill -f "$ROOT/node_modules/.bin/electron-vite"
pkill -f "$ROOT/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron"

```

`$ROOT` is the checkout's own path, read when the script runs, never written in. An earlier version had the old location hard-coded after the repository moved, so every kill matched nothing and the script started a second dev server, which quit at once on the single-instance lock. The path rule also stops a broader accident, because a bare `pkill -f "Electron.app"` matches every Electron app on the machine, ours among them.

When a path is not available, the bracket trick works. The regex `[e]lectron-vite` matches the text `electron-vite` in the target's arguments, but your own command line contains `[e]lectron-vite`, brackets included, which the regex does not match. We tested it with ancestors included:

```
$ bash -c "sleep 2; true" zzmark-q13 &
$ bash -c "pkill -a -f '[z]zmark-q13'; echo shell-survived"
shell-survived
target exit=143

```

The shell survived and the target was killed. Two more options are often better still: keep the process id when you start something and kill that id, or use `pkill -P $$` to signal only the current shell's own children.

## The same mistake with pgrep

`pgrep -f` matches the same way, and it has cost us more time than `pkill`. A loop waiting for `build-chromium.mjs sync` to finish polled `pgrep -f "build-chromium.mjs sync"`, and the loop's own command line contained the string. It waited for itself, and an hour passed with nothing running. The first time this happened it was a wait on `autoninja.py`.

Waiting on a log line removes the problem entirely:

```
until grep -q "has finished" sync.log; do sleep 10; done

```

The log is written by the thing you are waiting for and by nothing else, so it cannot match the waiter. Waiting on evidence the target produces, rather than on a guess, is also the point of [wait for a condition, not a sleep](https://mosaicbrowser.com/blog/wait-for-condition-not-sleep).

## Before you kill by pattern

Run `pgrep -lf "<pattern>"` first and read the list. If your own shell, your editor or a wrapper is on it, the pattern is too wide. Our rule now is a path only the target has, because when a wide pattern goes wrong it reports nothing, and a run that ends silently looks like a broken build. We already lose enough time to slow ones: an app release is [about seven minutes on a first run](https://mosaicbrowser.com/blog/electron-mac-build-seven-minutes), and an engine build has its own [disk and time budget](https://mosaicbrowser.com/blog/chromium-build-disk-budget).

## Sources

1. [pgrep(1), procps-ng, Linux manual page](https://man7.org/linux/man-pages/man1/pgrep.1.html), read November 3, 2026

## Questions

### Why does pkill -f kill my own shell?

Because -f matches the pattern against each process's full command line, and the shell running your command has your command, pattern included, as its argument. pkill never matches itself, but whether it skips the shell above it depends on the platform and its flags.

### Does pkill exclude its parent shell?

On macOS, by default it skips itself and all its ancestors; -a puts the ancestors back. Linux procps pkill skips only itself unless you pass -A or --ignore-ancestors. The same script can survive on one and die on the other.

### How do I kill a process by name without matching my own command?

Match a path only the target has, such as the full path of its binary inside your project. The bracket form, pkill -f '[e]lectron-vite', also works, because the regex matches the target but not the literal text in your own command line.

### What does exit code 137 mean?

128 plus 9: the process was ended by SIGKILL. A plain pkill sends SIGTERM, which shows as 143. Neither is an error from the program itself, so there is often nothing in its log.

## Related

[Seven minutes cold, three warm: timing a two-architecture Mac build](https://mosaicbrowser.com/blog/electron-mac-build-seven-minutes): Our two-architecture electron-builder Mac build took about 7 minutes cold and 3 warm. The gap was two Electron zips; the arm64 app was done 20 seconds early.

[What building Chromium costs in disk, and the estimate we retired](https://mosaicbrowser.com/blog/chromium-build-disk-budget): We budget 150 GB of free disk to check out and build Chromium on a Mac. Our time estimate of 4 to 7 hours was never measured; a rebuild took 93 minutes.

[A sleep is a guess with a number on it](https://mosaicbrowser.com/blog/wait-for-condition-not-sleep): Three detector rows slept for a time measured once; AmIUnique loaded in 31.7s once and under 10s the next. Polling for the value we read fixed all three.

[One pasted --disable-features deleted all of ours](https://mosaicbrowser.com/blog/disable-features-merge): Chromium reads only the last --disable-features on its command line. One extra flag silently re-enabled everything we turn off, so now we merge them.









[A regex that lost its backslashes inside a template literal](https://mosaicbrowser.com/blog/regex-in-template-literal): A template literal turns \s into s, so a regex shipped inside one matches nothing and throws nothing. It hid five of our detector rows as unread.

[Thirty tabs open: why a test suite needs a fresh tab per site](https://mosaicbrowser.com/blog/one-tab-per-detector): Captcha popups and ad windows left about 30 tabs open in our detector runner. Now each test gets its own tab, and a sweep closes every stray when it ends.

## Mosaic, on your own computer

The browser this blog is about: its own Chromium, fingerprints from real machines and a proxy IP per profile. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.

Mosaic is a desktop app

Email yourself the link and open it on your computer.
