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

00prometheus
  • 721
  • 5
  • 8

3 Answers3

0

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

justahuman
  • 153
  • 6
  • My problem isn't really how to store it, rather I am uncertain about *where* to store it. 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. – 00prometheus Jun 03 '20 at 10:53
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

binarysta
  • 2,912
  • 10
  • 14
  • 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. – 00prometheus Jun 03 '20 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.

David Kariuki
  • 777
  • 5
  • 23
  • 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. – 00prometheus Jun 03 '20 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? – David Kariuki Jun 03 '20 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)." – 00prometheus Jun 05 '20 at 11:04