PARALLEL(1) User Contributed Perl Documentation PARALLEL(1) NAME parallel - build and execute shell command lines from standard input in parallel SYNOPSIS parallel [-0cfgkquvmX] [-I str] [-j num] [--silent] [command [arguments]] [< list_of_arguments] DESCRIPTION For each line of input parallel will execute command with the line as arguments. If no command is given the line of input is executed. parallel can often be used as a substitute for xargs or cat | sh. Several lines will be run in parallel. command Command to execute. If command or the following arguments contain {} every instance will be substituted with the input line. Setting a command also invokes -f. If command is given, parallel will behave similar to xargs. If command is not given parallel will behave similar to cat | sh. --null -0 Use NUL as delimiter. Normally input lines will end in \n (newline). If they end in \0 (NUL), then use this option. It is useful for processing filenames that may contain \n (newline). --command -c Line is a command. The input line contains more than one argument or the input line needs to be evaluated by the shell. This is the default if command is not set. Can be reversed with -f. --delimiter delim -d delim Input items are terminated by the specified character. Quotes and backslash are not special; every character in the input is taken literally. Disables the end-of-file string, which is treated like any other argument. This can be used when the input consists of simply newline-separated items, although it is almost always better to design your program to use --null where this is possible. The specified delimiter may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code. Octal and hexadecimal escape codes are understood as for the printf command. Multibyte characters are not supported. --file -f Line is a filename. The input line contains a filename that will be quoted so it is not evaluated by the shell. This is the default if command is set. Can be reversed with -c. --group -g Group output. Output from each jobs is grouped together and is only printed when the command is finished. STDERR first followed by STDOUT. -g is the default. Can be reversed with -u. -I string Use the replacement string string instead of {}. --jobs N -j N --max-procs N -P N Run up to N jobs in parallel. 0 means as many as possible. Default is 10. --jobs +N -j +N --max-procs +N -P +N Add N to the number of CPUs. Run this many jobs in parallel. For compute intensive jobs -j +0 is useful as it will run number-of-cpus jobs in parallel. --jobs -N -j -N --max-procs -N -P -N Subtract N from the number of CPUs. Run this many jobs in parallel. If the evaluated number is less than 1 then 1 will be used. --jobs N% -j N% --max-procs N% -P N% Multiply N% with the number of CPUs. Run this many jobs in parallel. If the evaluated number is less than 1 then 1 will be used. --keeporder -k Keep sequence of output same as the order of input. If jobs 1 2 3 4 end in the sequence 3 1 4 2 the output will still be 1 2 3 4. --quote -q Quote command. This will quote the command line so special characters are not interpreted by the shell. See the section QUOTING. Most people will never need this. Quoting is disabled by default. --silent Silent. The job to be run will not be printed. This is the default. Can be reversed with -v. --ungroup -u Ungroup output. Output is printed as soon as possible. This may cause output from different commands to be mixed. Can be reversed with -g. -v Verbose. Print the job to be run on STDOUT. Can be reversed with --silent. --xargs -m Multiple. Insert as many arguments as the command line length permits. If {} is not used the arguments will be appended to the line. If {} is used multiple times each {} will be replaced with all the arguments. -X xargs with context replace. This works like -m except if {} is part of a word (like pic{}.jpg) then the whole word will be repeated. EXAMPLE 1: Working as cat | sh. Ressource inexpensive jobs and evaluation parallel can work similar to cat | sh. A ressource inexpensive job is a job that takes very little CPU, disk I/O and network I/O. Ping is an example of a ressource inexpensive job. wget is too - if the webpages are small. The content of the file jobs_to_run: ping -c 1 10.0.0.1 wget http://status-server/status.cgi?ip=10.0.0.1 ping -c 1 10.0.0.2 wget http://status-server/status.cgi?ip=10.0.0.2 ... ping -c 1 10.0.0.255 wget http://status-server/status.cgi?ip=10.0.0.255 To run 100 processes simultaneously do: parallel -j 100 < jobs_to_run As there is not a command the option -c is default because the jobs needs to be evaluated by the shell. EXAMPLE 2: Working as xargs -n1. Argument appending parallel can work similar to xargs -n1. To output all html files run: find . -name '*.html' | parallel cat As there is a command the option -f is default because the filenames needs to be protected from the shell in case a filename contains special characters. EXAMPLE 3: Compute intensive jobs and substitution If ImageMagick is installed this will generate a thumbnail of a jpg file: convert -geometry 120 foo.jpg thumb_foo.jpg If the system has more than 1 CPU it can be run with number-of-cpus jobs in parallel (-j +0). This will do that for all jpg files in a directory: ls *.jpg | parallel -j +0 convert -geometry 120 {} thumb_{} To do it recursively use find: find . -name '*.jpg' | parallel -j +0 convert -geometry 120 {} {}_thumb.jpg Notice how the argument has to start with {} as {} will include path (e.g. running convert -geometry 120 ./foo/bar.jpg thumb_./foo/bar.jpg would clearly be wrong). It will result in files like ./foo/bar.jpg_thumb.jpg. If that is not wanted this can fix it: find . -name '*.jpg' | \ perl -pe 'chomp; $a=$_; s:/([^/]+)$:/thumb_$1:; $_="convert -geometry 120 $a $_\n"' | \ parallel -c -j +0 Unfortunately this will not work if the filenames contain special characters (such as space or quotes). If you have ren installed this is a better solution: find . -name '*.jpg' | parallel -j +0 convert -geometry 120 {} {}_thumb.jpg find . -name '*_thumb.jpg' | ren 's:/([^/]+)_thumb.jpg$:/thumb_$1:' EXAMPLE 4: Substitution and redirection This will compare all files in the dir to the file foo and save the diffs in corresponding .diff files: ls | parallel diff {} foo ">"{}.diff Quoting of > is necessary to postpone the redirection. Another solution is to quote the whole command: ls | parallel "diff {} foo >{}.diff" EXAMPLE 5: Composed commands A job can consist of several commands. This will print the number of files in each directory: ls | parallel 'echo -n {}" "; ls {}|wc -l' To put the output in a file called .dir: ls | parallel '(echo -n {}" "; ls {}|wc -l) > {}.dir' EXAMPLE 6: Context replace To remove the files pict0000.jpg .. pict9999.jpg you could do: seq -f %04g 0 9999 | parallel rm pict{}.jpg You could also do: seq -f %04g 0 9999 | perl -pe 's/(.*)/pict$1.jpg/' | parallel -m rm The first will run rm 10000 times, while the last will only run rm as many times needed to keep the command line length short enough (typically 1-2 times). You could also run: seq -f %04g 0 9999 | parallel -X rm pict{}.jpg This will also only run rm as many times needed to keep the command line length short enough. EXAMPLE 7: Group output lines When runnning jobs that output data, you often do not want the output of multiple jobs to run together. parallel defaults to grouping the output of each job, so the output is printed when the job finishes. If you want the output to be printed while the job is running you can use -u. Compare the output of: (echo foss.org.my; echo debian.org; echo freenetproject.org) | parallel traceroute to the output of: (echo foss.org.my; echo debian.org; echo freenetproject.org) | parallel -u traceroute EXAMPLE 8: Keep order of output same as order of input Normally the output of a job will be printed as soon as it completes. Sometimes you want the order of the output to remain the same as the order of the input. -k will make sure the order of output will be in the same order as input even if later jobs end before earlier jobs. (echo foss.org.my; echo debian.org; echo freenetproject.org) | parallel traceroute will give traceroute of foss.org.my, debian.org and freenetproject.org, but it will be sorted according to which job completed first. To keep the order the same as input run: (echo foss.org.my; echo debian.org; echo freenetproject.org) | parallel -k traceroute This will make sure the traceroute to foss.org.my will be printed first. QUOTING For more advanced use quoting may be an issue. The following will print the filename for each line that has exactly 2 columns: perl -ne '/^\S+\s+\S+$/ and print $ARGV,"\n"' file This can be done by parallel using: ls | parallel "perl -ne '/^\\S+\\s+\\S+$/ and print \$ARGV,\"\\n\"'" Notice how \'s, "'s, and $'s needs to be quoted. parallel can do the quoting by using option -q: ls | parallel -q perl -ne '/^\S+\s+\S+$/ and print $ARGV,"\n"' However, this means you cannot make the shell interpret special characters. For example this will not work: ls | parallel -q "diff {} foo >{}.diff" ls | parallel -q "ls {} | wc -l" because > and | need to be interpreted by the shell. If you get errors like: sh: -c: line 0: syntax error near unexpected token then you might try using -q. If you are using bash process substitution like <(cat foo) then you may try -q and prepending command with bash -c: ls | parallel -q bash -c 'wc -c <(echo {})' Or for substituting output: ls | parallel -q bash -c 'tar c {} | tee >(gzip >{}.tar.gz) | bzip2 >{}.tar.bz2' Conclusion: To avoid dealing with the quoting problems it may be easier just to write a small script and have parallel call that script. LIST RUNNING JOBS If you want a list of the jobs currently running you can run: killall -USR1 parallel parallel will then print the currently running jobs on STDERR. COMPLETE RUNNING JOBS BUT DO NOT START NEW JOBS If you regret starting a lot of jobs you can simply break parallel, but if you want to make sure you do not have halfcompleted jobs you should send the signal SIGTERM to parallel: killall -TERM parallel This will tell parallel to not start any new jobs, but wait until the currently running jobs are finished. DIFFERENCES BETWEEN xargs/find -exec AND parallel xargs and find -exec offer some of the same possibilites as parallel. find -exec only works on files. So processing other input (such as hosts or URLs) will require creating these inputs as files. find -exec has no support for running commands in parallel. xargs deals badly with special characters (such as space, ' and "). To see the problem try this: touch important_file touch 'not important_file' ls not* | xargs rm mkdir -p '12" records' ls | xargs rmdir You can specify -0 or -d "\n", but many input generators are not optimized for using NUL as separator but are optimized for newline as separator. E.g head, tail, awk, ls, echo, sed, tar -v, perl (-0 and \0 instead of \n), locate (requires using -0), find (requires using -print0), grep (requires user to use -z or -Z). So parallel's newline separation can be emulated with: cat | xargs -d "\n" -n1 command xargs can run a given number of jobs in parallel, but has no support for running no_of_cpus jobs in parallel. xargs has no support for grouping the output, therefore output may run together, e.g. the first half of a line is from one process and the last half of the line is from another process. xargs has no support for keeping the order of the output, therefore if running jobs in parallel using xargs the output of the second job cannot be postponed till the first job is done. xargs has no support for context replace, so you will have to create the arguments. If you use a replace string in xargs (-I) you can not force xargs to use more than one argument. Quoting in xargs works like -q in parallel. This means composed commands and redirection requires using bash -c. ls | parallel "wc {} > {}.wc" becomes ls | xargs -d "\n" -P10 -I {} bash -c "wc {} > {}.wc" and ls | parallel "echo {}; ls {}|wc" becomes ls | xargs -d "\n" -P10 -I {} bash -c "echo {}; ls {}|wc" DIFFERENCES BETWEEN mdm/middleman AND parallel middleman(mdm) is also a tool for running jobs in parallel. Here are the shellscripts of http://mdm.berlios.de/usage.html ported to parallel use: seq 1 19 | parallel -j+0 buffon -o - | sort -n > result cat files | parallel -j+0 cmd BUGS Filenames beginning with '-' can cause some commands to give unexpected results, as it will often be interpreted as an option. REPORTING BUGS Report bugs to . IDEAS Test if -0 works on filenames ending in '\n' xargs dropin-replacement. Implement the missing --features monitor to see which jobs are currently running http://code.google.com/p/ppss/ Accept signal INT instead of TERM to complete current running jobs but do not start new jobs. Print out the number of jobs waiting to complete on STDERR. Accept sig INT again to kill now. This seems to be hard, as all foreground processes get the INT from the shell. If there are nomore jobs (STDIN is closed) then make sure to distribute the arguments evenly if running -X. Distibute jobs to computers with different speeds/no_of_cpu using ssh ask the computers how many cpus they have and spawn appropriately according to -j setting. Reuse ssh connection (-M and -S) http://www.semicomplete.com/blog/geekery/distributed-xargs.html?source=rss20 http://code.google.com/p/ppss/wiki/Manual2 -S -S sshlogin[,sshlogin] sshlogin is [user@]host or filename with list of sshlogin What about copying data to remote host? Have an option that says the argument is a file that should be copied. What about copying data from remote host? Have an option that says the argument is a file that should be copied. Where will '>' be run? Local or remote? Parallelize so this can be done: mdm.screen find dir -execdir mdm-run cmd {} \; Maybe: find dir -execdir parallel --communication-file /tmp/comfile cmd {} \; Comfile This will put a lock on /tmp/comfile. The number of locks is the number of running commands. If the number is smaller than -j then it will start a process in the background ( cmd & ), otherwise wait. parallel --wait /tmp/comfile will wait until no more locks on the file AUTHOR Copyright (C) 2007-10-18 Ole Tange, http://ole.tange.dk Copyright (C) 2008-2010 Ole Tange, http://ole.tange.dk LICENSE Copyright (C) 2007-2010 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or at your option any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . DEPENDENCIES parallel uses Perl, and the Perl modules Getopt::Long, IPC::Open3, Symbol, IO::File, POSIX, and File::Temp. SEE ALSO find(1), xargs(1) perl v5.10.1 2010-03-06 PARALLEL(1)