Technology1 min read

Concurrent Shell Command Queues

By · Published by Everything Blog

In short

The architecture establishes a dedicated thread pool by combining a background `xargs` process with a named FIFO file stored in a temporary directory. An initialization routine validates the concurrency limit and assigns an unused file descriptor between 3 and 9 to process incoming NUL-separated commands. Tasks are subsequently written to this descriptor over time and adapt execution depending on whether the shell is interactive. A closing function then closes the file descriptor and monitors the background process until all queued operations conclude.

Key points

  • Native Bash scripts can achieve controlled parallel execution without relying on outside…: Native Bash scripts can achieve controlled parallel execution without relying on outside languages like Python.
  • Commands can be submitted dynamically across runtime rather than requiring a static list…: Commands can be submitted dynamically across runtime rather than requiring a static list upfront.
  • Task execution automatically adjusts between foreground and background modes depending on…: Task execution automatically adjusts between foreground and background modes depending on shell interactivity.

Sources checked by our analysis pipeline: 1

How to run shell commands in parallel while limiting their concurrency.

I had a use case recently where I wanted to speed up some Bash processing that I was doing, but still keep concurrency constrained. So I wondered: is there a straightforward way to create thread pools in Bash? The answer is: yes, but it takes some work to be semi-resilient and portable.

You may ask: why do this in Bash at all? It seems silly to use a language with poor concurrency controls. You'd be right to ask this question, and my answer is: because I have a fondness for Bash, and I wanted the challenge. But you're still probably better off using another language, such as Python.

Here is code that creates a command queue that accepts shell commands to execute. You do not need to know all the commands that you want executed up-front, you can submit commands to it over time.

1# Start this shell's command queue.

2# @param {number} $1 Maximum number of jobs to run at once

3queue_start() {

4 # Validate that the…

Technology