One writer at a time, and a file that is only ever whole — cheat sheet
python, boot-systemd
import fcntl, os, tempfile, json
fd = os.open(LOCK, os.O_CREAT | os.O_RDWR, 0o644) # keep fd open for the whole run
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
sys.exit("another run holds the lock")
tmp_fd, tmp = tempfile.mkstemp(dir=os.path.dirname(OUT), prefix=".prices-")
with os.fdopen(tmp_fd, "w", encoding="utf-8") as f:
json.dump(prices, f); f.flush(); os.fsync(f.fileno())
os.chmod(tmp, 0o644)
os.replace(tmp, OUT) # atomic within one file system
flock -n /run/job.lock -c 'command' # the same lock, from a shell
systemctl list-timers --no-pager # what is due, and when it last ran
systemctl show unit -p Result -p ExecMainStatus