One of the great things about Unix is the ability for users to add small tools that fit in seamlessly with those provided by the base operating system. I’ve written a few such tools over the years.
Today I’m releasing another little Unix tool try_repeat, which
tries to run a command n times, exiting early if the command exits
with a non-zero exit code. Think of it a bit like the repeat
command built into some shells (e.g. zsh) but which “exits as soon
as an execution of the command fails”. This is particularly useful when trying
to find intermittent failures in a command: I can set an upper bound (i.e. “if
the command runs n times successfully, I’ll assume there are no
problems”) but be notified if there are any problems earlier on.
I’ve had a basic version of this command knocking around in my
bin/ directory for quite a while. Earlier today I was trying to
debug a non-deterministic failure and fiddling around with unwieldy shell
commands. Once I rediscovered try_repeat, it allowed
me to quickly hunt down the non-deterministic failure, fix it, and feel
confident that I’d fixed it.
That was all the motivation I needed to tidy things up, extend them a bit, and make a formal release. With a new name1, I’ve now released try_repeat 0.1.0.
try_repeat is a deliberately simple tool as you can see from
the examples:
$ try_repeat -h Usage: try_repeat [-hv] <count> <command> [<arg1> ... <argn>] $ try_repeat 3 ls /etc/motd /etc/motd /etc/motd /etc/motd $ echo $? 0 $ try_repeat 3 ls /doesntexist ls: /doesntexist: No such file or directory $ echo $? 1 $ try_repeat -v 3 ls /etc/motd ===> 1: ls /etc/motd /etc/motd ===> 2: ls /etc/motd /etc/motd ===> 3: ls /etc/motd /etc/motd
As with the first release of any bit of software, I can’t promise that
try_repeat is perfect, so please let me know of any problems!
Footnotes
This tool was originally called repeat_or but Edd Barrett came up
with the much better name of try_repeat.
This tool was originally called repeat_or but Edd Barrett came up
with the much better name of try_repeat.