This is my specific problem I need to solve (But my question is different, so please read on even if you don't know what fail2ban is):
I'm trying to use fail2ban on a linux server to ban brute force attacks on multiple services (ssh, dovecot, apache, postfix, ...). Now I stumbled over the problem that fail2ban seems to execute multiple iptables commands at the same time (with threads in Python) and this fails pretty often (Especially on startup) where iptables returns "Resource temporarily unavailable." errors.
I want to workaround these problems by "synchronizing" calls to iptables. I'm searching for a program which creates some mutex/lock file and only if this file could be created then it starts the real command and removes the mutex file after the command has finished. If the mutex file is already there then the program waits until the mutex file is gone and then tries to aquire it.
With such a command I could configure the iptables-actions in fail2ban to wait for each other so they don't execute at the same time.
I'm pretty sure there is already such a program out there so I don't have to write this on my own. But so far I haven't found it.