Tired of commands running for hours only to fail with an error?
@techwithdilan · · 2 min read
So, you have been running "npm run dev" for hours again and again just to come 3 hours after and realize that it has failed?
Well you are not alone in this situation, and most of the time the faillure is coming from a slow network conection, rate limit, or other temporary problems.
Today we will have a look at a simple Bash script that automatically retries failed commands a specified number of times before giving up.
Automatically retries commands when they fail
Configurable retry count (defaults to 5 attempts)
Clear feedback during execution
Proper exit codes (0 on success, 1 on failure)
Installation
After cloning this repository, follow these steps to make the script available globally:
On Unix-based systems (Linux, macOS)
# Make the script executable
chmod +x retry-command.sh
# Move to a directory in your PATH
sudo cp retry-command.sh /usr/local/bin/retry-commandOn Windows with Git Bash
# Create a personal bin directory if it doesn't exist
mkdir -p ~/bin
# Copy the script and make it executable
cp retry-command.sh ~/bin/retry-command
chmod +x ~/bin/retry-command
# Add to PATH in your .bashrc
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrcUsage
# Basic usage
retry-command "your-command-here"
# With custom retry count
retry-command "your-command-here" 10
# Examples
retry-command "git push" 5
retry-command "npm test" 3
retry-command "curl -f https://api.example.com/health"
retry-command "docker build -t myapp ."How It Works
The script will:
Run your command
If it fails, wait 3 seconds and try again(you can tweak this easly by modifying the .sh file)
Continue until success or max retries is reached (don't exaggerate too much with the max retries 🤪)
Report the final outcome
Note: The code is simple so that everyone can customize it the way they want, so feel free to open an issue or a PR to the repohere
3 Replies5 Likes






