2

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).

3 Answers 3

1

Have you considered simply storing the value in a file? A file, for this purpose, can replace a database. I am however concerned of reading a file 100 times per second and the race conditions for your script. But you can use the following:

Run 1:

var=2
echo $var > foo.txt

Run 2:

var=$(head -n 1 foo.txt)
echo $var    # 2

You can also use sed to do more complex processing. Furthermore, if your script is sequential, you can simply append to file using >> and read the last line each run (using tail) to combat locking. But I believe a better design would be to maybe run your script in a for loop rather than calling the script 100 times (if possible).

1
  • I was concerned about writing to my SSD so often, but clearly, if I want the value to persist through machine crashes I have to store the value to disk at least often enough to afford losing what I haven't written. Mar 5 at 20:46
0

I would suggest store it in a variable and write it to file in specific occasions (like EXIT, TERM). you can use trap to run commands based on the signals received by the process.

example at trap command

1
  • Storing on a trap won't help me (I do this already, but that isn't my issue). What I am trying to do is to guard against a machine crash, so the trap might not execute. Jun 3, 2020 at 10:54
0

The best way is storing the counters last value in a file in specific occasions like on EXIT. This is simpler than trying to use builtin options You would then include the path to the file in your script, get value and resume when the script restarts.

you can use trap to run commands based on the signals received by the process.

3
  • Storing on a trap won't help me (I do this already, but that isn't my issue). What I am trying to do is to guard against a machine crash, so the trap might not execute. Jun 3, 2020 at 10:52
  • Please update or clarify your problem more clearly. What do you want to handle or catch in case of a system crash? Jun 3, 2020 at 17:25
  • Does this help? "The script works fine, but I would like the counter to persist on machine crashes." Also: "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)." Jun 5, 2020 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .