Benjamin Schieder

MULTIPROCESSING SHELL SCRIPTS

2013 July 11

While working on the Project Zomboid Map Project I’ve come to a situation where I had lots of identical jobs to do but the tool to do it wasn’t multithreading/-processing.
I’ve overcome it this way:

for THREAD in 0 1 2 3 ; do
	(
		JOB=0
		for file in *bmp ; do
			JOB=$(( ${JOB} + 1 ))
			[ $(( ${JOB} % 4 )) -eq ${THREAD} ] || continue
			convert "${file}" png:"${file%bmp}png"
		done
	) &
done
wait

It’s not perfect, for example if one thread finishes its jobs earlier than the rest then it is just idling, but it works fine otherwise.

EOF

Category: blog

Tags: bash shell multiprocessing