I want to know how I can send the command(s) spawned by xargs to background. For example, consider
find 开发者_Go百科. -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?
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)
You could probably make a very quick shellscript to call compress.
#!/bin/sh
# call it 'compbg' and chmod a+x
compress $* &
then
find . -type f -mtime +7 | tee compressedP.list | xargs -I{} compbg {}
Although I think you might be happier using this xargs argument:
-P maxprocs
Parallel mode: run at most maxprocs invocations of utility at once.
This command should find / tee / compress 10 files at a time until its done, as well as returning control immediately to the calling script/shell.
find . -type f -mtime +7 | tee compressedP.list | xargs -I{} -P10 compress {} &
For SunOS you may have a look at GNU Parallel http://www.gnu.org/software/parallel/
find . -type f -mtime +7 | tee compressedP.list | parallel compress
It has the added benefit of not terminating incorrectly if the filename contains ' " or space. Adding -j+0 will make it run one compress per CPU core.
This works for me when I want to edit all files that contain foo
grep -rl foo ./ | `xargs emacs` &
On SunOS/Solaris, wrap the command in a call to sh (or other shell):
find . -type f -mtime +7 | tee compressedP.list | xargs -Ifname sh -c 'compress fname &'
This should run the compresses in parallel without additional tools.
精彩评论