Execute script on shutdown (openhabian / linux)

Hi all,
I am running openHAB 2.5.2-1 (Release Build) on raspi 3 / openhabian / Raspbian GNU/Linux 10

Trying to execute some commands / a script on Linux shutdown I was not successful so far.
I use runlevel system:
script is in /etc/init.d/ (a bash-script without .sh like the other scripts there, file rights exactly as the other scripts there, executable of course, same user / group)
-rwxr-xr-x 1 root root 884 Mar 23 21:47 writeback-persist

Then I created symlinks in /etc/rc6.d and /etc/rc0.d
lrwxrwxrwx 1 root root 29 Mar 23 22:54 K00aapersist -> /etc/init.d/writeback-persist

The script just consists of some tests to check if it runs:
echo “Start writeback from init.d” >> /var/log/test.log
date >> /var/log/test.log
cat /var/log/openhab2/openhab.log >> /var/log/test.log

If I reboot using shutdown -r now
the logs afterwards are not written.

I even put the same few commands into an existing script in /etc/init.d but also then the test.log is not written.
Needless to say that the script runs perfect if executed manually … or if I put the same statements into /etc/rc.local (but then they get executed during startup, not shutdown which is the wrong timing…)

I would appreciate very much some ideas what could be the solution …


By “Raspbian GNU/Linux 10” you mean Raspbian Buster, than you need to be using systemd and not init.d. See https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown and google for general information about systemd.

As Rich correctly stated you should go understand systemd first.
Then again I suspect being victim to the XY problem
Please state your problem X, not your attempted solution Y:
why at all do you need your script - to have OH log to memory and then sync to disk ?
If that is what you are looking for, just enable ZRAM then that’s what openHABian will do for you by default.

Dear @rlkoshak and @mstormi,
Thanks a lot for your replies. Overall goal is to get less impact on the SD card.
I was not yet discovering ZRAM (I will check this of course).
I tried to adopt some youtoube where they use tmpfs, rrd4j and mapdb for persistence.
While preserving config, they just write Logs into tmpfs and delete them regularly using crone job. So I searched for a solution for Logs to have them in the ram disc and preserving them at least when rebooting / shutting down.
Both answers take me a step further, thanks a lot again!