2014 Plan  • • •  Steve Jobs and Bill Gates       all posts in Archive

Threads in Bash

The title 'Threads in Bash' is incorrect ^_^ There's no thread when creating BASH Script, I just find out these days when I trying to make a script to update ZIMBRA accounts by Bash.

Recently when we creating scripts in linux, most people will choose Python and few person will use Perl. But why I use BASH this time ... ???  ^o^ In fact, I don't know, maybe just cause it's a little funny to use some Bash some time. And I really quite enjoyed in that time. And At the beginning, I didn't think will make a so sophisticated script at the end.

After finished the first version, I find there's so many accounts, so if the script can't be continuable, it's unacceptable. After that, it's too slow to update, costing few days. So make it able to run with several threads is a good idea. However, I have said, there's no Threads in BASH, what a awkward ...

The only choose is using multiple processes, and infact it's quite easy to do it, just like:

COMMAND A & COMMAND B & COMMAND C & 
WAIT
  • &  as we know, it means the process is forked and shell will run it in the backend in a sub shell
  • and WAIT will make sure waiting for  all the processes to finish

P.S.

However, after finished these scripts. It's still not good enough. It's still too slow.

And even using real thread can't help too much. The time cost mostly in the cmd tool ZMPROV provided by Zimbra.

So the best way could be creating you own ZMPROV  wapper, then you can do batch update and avoid wasting time in creating and release session.