try_repeat released

January 25 2023

Blog archive

Last 10 blog posts
My Interview with Eelco Visser on Parsing
Why Split Lexing and Parsing Into Two Separate Phases?
Displaying My Washing Machine's Remaining Time With curl, jq, and pizauth
pizauth: dump and restore
How Big Should a Programming Language Be?
Rust's Two Kinds of 'Assert' Make for Better Code
Scheduling my Electricity Usage
Why Aren't Programming Language Specifications Comprehensive?
Distinguishing an Interpreter from a Compiler
try_repeat released
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 name [1], 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
$ echo $?
$ try_repeat 3 ls /doesntexist
ls: /doesntexist: No such file or directory
$ echo $?
$ try_repeat -v 3 ls /etc/motd
===> 1: ls /etc/motd
===> 2: ls /etc/motd
===> 3: ls /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!

If you’d like updates on new blog posts: follow me on Mastodon or Twitter; or subscribe to the RSS feed; or subscribe to email updates:


[1] 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.


(used only to verify your comment: it is not displayed)

Can't load comments