DEV Community

Cover image for Bringing timeout to macOS Without Installing All of GNU Coreutils
Maple
Maple

Posted on

Bringing timeout to macOS Without Installing All of GNU Coreutils

Not long ago, I was writing a shell script on macOS and ran into a small but surprising issue: there was no built-in timeout command. If you’ve spent time on Linux, you’ve probably taken timeout for granted, it’s part of GNU coreutils and is incredibly handy when you want to limit how long a command can run.

On macOS, though, the situation is different. macOS ships with a BSD-flavored set of core utilities, and GNU’s version of timeout simply isn’t there. My first instinct was to install GNU coreutils through Homebrew:

brew install coreutils
Enter fullscreen mode Exit fullscreen mode

This does work—but it comes with a catch. To avoid conflicts with the BSD commands that ship with macOS (like ls and cat), Homebrew installs GNU coreutils under names prefixed with a g. So instead of ls, you get gls; instead of timeout, you get gtimeout.

That’s fine if you’re working in a personal environment, but it creates headaches for writing portable shell scripts. If I write a script that calls gtimeout on macOS, it won’t run out-of-the-box on Linux, where the command is just timeout. Homebrew does provide a way to remove the g prefix, but that’s a pretty heavy-handed solution—suddenly your system’s default ls, cat, and other tools are replaced by GNU versions, which can break expectations in subtle ways.

So, instead of going down that path, I decided to build a standalone timeout command that behaves like GNU’s version but doesn’t bring along the entire coreutils package. This way, you can install just what you need without worrying about side effects.

The project is open source and lives here:
👉 https://github.com/aisk/timeout

And to make installation easier, I also set up a Homebrew tap. You can install it directly with:

brew install aisk/homebrew-tap/timeout
Enter fullscreen mode Exit fullscreen mode

That’s it—you’ll get a drop-in replacement for timeout, without the g prefix, and without swapping out your other system tools.

I built this mainly to solve my own problem, but if it helps you too, I’d really appreciate a star on the GitHub repo. Every bit of support means a lot and helps me keep the project alive.

Top comments (0)