4

I have the following snippet in my bash script

#!/bin/bash
    for ((i=100; i>=70; i--))
      do
        convert test.png -quality "$i" -sampling-factor 1x1 test_libjpeg_q"$i".jpg
      done

How can i execute the for loop in parallel using all cpu cores.I have seen gnu parallel being used but here i need the output filename in a specific naming scheme as shown above

user2650277
  • 6,289
  • 17
  • 63
  • 132
  • turn your `convert ..` line into a separate shell script that `parallel` can call . Good luck. – shellter Aug 28 '17 at 17:10
  • Append a blank and `&` to convert command. – Cyrus Aug 28 '17 at 17:13
  • 2
    Note that, if performance is your primary concern, and depending on the ratio of the sizes of each image to the total number of images, it may be better to use `mogrify` and **GNU Parallel**'s `-X` option because if you use `convert`, you necessarily have to create a whole new process for every image. So if you have a large number of small images, the overhead is enormous. Say you have 80,000 images, you will be better off with 8 `mogrify` processes doing 10,000 images each, than 80,000 `convert` processes doing 1 each. – Mark Setchell Aug 30 '17 at 07:41

1 Answers1

6

You can use parallel like this:

parallel \
'convert test.png -quality {} -sampling-factor 1x1 test_libjpeg_q{}.jpg' ::: {100..70}
anubhava
  • 761,203
  • 64
  • 569
  • 643