1

I have a loop that cycles through a bunch of images that I need to edit with Imagemagick. Trouble is, it's slow to have to wait for each image to be edited before editing the next one. I want to execute each of the convert commands asynchronously not waiting for the last one to finish.Is this possible with bash? Here's a basic overview of what I have:

for img in *
do
  convert $img **more params here**
done

I want to execute that convert command asynchronously. So I can convert all the images at once. Is this possible with bash?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • If you have GNU `xargs`, you could use it for running multiple commands in parallel. Check this post: http://stackoverflow.com/questions/28357997/running-programs-in-parallel-using-xargs – codeforester Mar 08 '17 at 06:27
  • How does your system behave? resource-wise? is it capable of running commands in parallel? – Inian Mar 08 '17 at 06:30
  • Replace `convert $img **more params here**` by `convert $img **more params here** &` to convert **all** parallel. – Cyrus Mar 08 '17 at 06:33
  • @Inian To be honest with you I don't really know how to check that. It has plenty of horsepower behind it –  Mar 08 '17 at 06:36
  • 1
    @AlexKonetchy: Refer the simplest ways to achieve parallelism by doing as in Cyrus' comment above, or if you are more specific tools use the link shared in the first comment. – Inian Mar 08 '17 at 06:38
  • Okay, I guess the only added complexity is I need to know when the last convert $img & has finished. What's the best way to check that? –  Mar 08 '17 at 06:40
  • @AlexKonetchy: add `wait` in an extra line after `done`. – Cyrus Mar 08 '17 at 06:48
  • Thank you! If you add an answer Cyrus, I'll accept it :) –  Mar 08 '17 at 06:54

1 Answers1

3

If you have many images, running hundreds/thousands of convert processes in parallel is not going to work very well and you would be better off with GNU Parallel which will keep all your CPU cores busy without overloading the system - so if you have 8 cores, it will do 8 images at a time (though you can change that).

So, if you wanted to resize all the JPG images in your directory down to half their original size and rename them resized-XYZ.jpg:

parallel convert {} -resize 50% resized-{} ::: *.jpg

If you want to do all the JPG files and the PNG files and see a progress meter as they run:

parallel --progress convert {} -resize 50% resized-{} ::: *.jpg *.png

If you want to do specifically 8 at a time, use:

parallel -j8 ....

If you want to see what the command is going to do, without actually doing anything:

parallel --dry-run ...
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432