I have a simple bash script that increments a counter a few times per second, guaranteed less than 100 times per second. The script works fine, but I would like the counter to persist on machine crashes.
What would be the best way to persist the counter on my SSD-only system? Should I just echo it out to /var/<app>/ somewhere (i.e. store in a file) each time it updates? If so, is /var/<app>/ the right place? Do I need to install a full database to keep track of this single value? Is there some cute little Linux feature built to do this effectively?
To clarify, my problem isn't making sure that the counter is persistent between separate runs of the script, I have that solved already. My concern is in case the system unexpectedly and suddenly fails due to machine crash (I can therefore not rely on a trap in a shell script).