Skip to content
Latchkey

xargs -P: Run Commands in Parallel

xargs -P N runs up to N command invocations at the same time, parallelizing work across cores.

When each item is independent, -P turns a serial loop into a parallel one. It is the simplest way to saturate a multi-core runner without writing a job queue.

What it does

xargs -P <max-procs> runs up to max-procs invocations of the command concurrently. It only helps if the input is split into multiple invocations, so you pair -P with -n (items per run) or -I (one per run). With -P 0, GNU xargs runs as many processes as possible at once.

Common usage

Terminal
# 4 parallel image conversions, one file each
find . -name '*.png' -print0 | xargs -0 -P 4 -n 1 optipng
# parallelize per-item commands with -I
cat hosts.txt | xargs -P 8 -I {} ssh {} uptime

Options

FlagWhat it does
-P <max>Run up to max command processes in parallel
-P 0GNU: run as many as possible at once
(with -n)Each process handles a batch of n items
(with -I {})Each process handles one item

In CI

Set -P to the core count, e.g. -P "$(nproc)" on Linux runners. Output from parallel processes interleaves and can be garbled line by line; if you need clean logs, have each command write to its own file. xargs -P returns a non-zero exit status if any invocation fails, which keeps the step honest.

Common errors in CI

Without -n or -I, -P has nothing to parallelize because all items go into a single invocation, so it silently runs serially. Interleaved or corrupted stdout is expected under -P; it is not a crash. If processes exhaust memory, lower -P. On very old xargs builds -P is unsupported and prints "invalid option".

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →