Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending the command(s) spawned by xargs to background

I want to know how I can send the command(s) spawned by xargs to background. For example, consider

find . -type f  -mtime +7 | tee compressedP.list | xargs compress

I tried

find . -type f  -mtime +7 | tee compressedP.list | xargs -i{} compress {} &

.. and as unexpected, it seems to send xargs to the background instead?

How do I make each instance of the compress command go to the background?

like image 712
PoorLuzer Avatar asked Dec 23 '09 11:12

PoorLuzer


People also ask

What does xargs command do?

The xargs command builds and executes commands provided through the standard input. It takes the input and converts it into a command argument for another command. This feature is particularly useful in file management, where xargs is used in combination with rm , cp , mkdir , and other similar commands.

What is the default command used by xargs?

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is echo) one or more times with any initial-arguments followed by items read from standard input.

What is xargs I option?

The -I option changes the way the new command lines are built. Instead of adding as many arguments as possible at a time, xargs will take one name at a time from its input, look for the given token ( {} here) and replace that with the name.

What does piping to xargs do?

xargs is a Unix command used to build and execute commands from the standard input. You can combine it with other powerful Unix commands, such as grep , awk , etc., by using pipes. In simple terms, it passes the output of a command as the input of another. It works on most Unix-like operating systems.


1 Answers

Use the --max-procs / -P option to run xargs targets in the background. From the man page of GNU xargs version 4.2.27:

--max-procs=max-procs, -P max-procs

Run up to max-procs processes at a time; the default is 1. If max-procs is 0, xargs will run as many processes as possible at a time. Use the -n option with -P; otherwise chances are that only one exec will be done.

(You may want to combine this with -n 1 to makes sure that there is a new process for each file you want to compress)

like image 73
RobM Avatar answered Sep 26 '22 09:09

RobM