How to write idempotent Bash scripts
arslan.io
How to write idempotent Bash scripts
1–10 of 195 posts
Re: How to write idempotent Bash scripts
#2Re: How to write idempotent Bash scripts
#3Re: How to write idempotent Bash scripts
#4Then you have scripts that you want run once and if run again not have any more effect. FOr example a script to be run by ops on production servers, what happens if they accidently run it twice! An easy way to cover that is to have the script create a file, which you check at the start of the script and if present you exit. For this you can use the date/time, PID and script name for a unique file name, create in a /tmp directory that you cleanup weekly via sculker or whatever frequency you require. Handy way to handle scripts that you want run successfully once and never to be run again.
Re: How to write idempotent Bash scripts
#5The problem with the idempotent trend in configuration management is that it’s all based on not tracking or knowing what the current state of something is. So reasoning about how these systems work is fundamentally impossible. It would be better to focus on systems where by we can always know the state and improve the tooling there.
Re: How to write idempotent Bash scripts
#6The problem with the idempotent trend in configuration management is that it’s all based on not tracking or knowing what the current state of something is. So reasoning about how these systems work is fundamentally impossible. It would be better to focus on systems where by we can always know the state and improve the tooling there.
With the default behaviour of rm, ln -s, etc you know neither the state before, nor after.
Re: How to write idempotent Bash scripts
#7Why wouldn’t you use make? Side-effect tracking and cleanup is the job of a build tool.
Re: How to write idempotent Bash scripts
#8Re: How to write idempotent Bash scripts
#9Why wouldn’t you use make? Side-effect tracking and cleanup is the job of a build tool.
Why would you use a build tool when you're not writing a build script?
Re: How to write idempotent Bash scripts
#10The problem with the idempotent trend in configuration management is that it’s all based on not tracking or knowing what the current state of something is. So reasoning about how these systems work is fundamentally impossible. It would be better to focus on systems where by we can always know the state and improve the tooling there.
For the case of a setup script you'd need to update your state file after every step and in turn when running it again see what the file says and resume at whatever point it says. But then you risk running into above problem.
I try to write idempotent scripts whenever possible, combined with general sanity checks specific to whatever environment the script expects.