init_commands:
- if [ ! -f /backup/_swap.swap ]; then fallocate -l 2G /backup/_swap.swap && mkswap /backup/_swap.swap && chmod 0600 /backup/_swap.swap && swapon /backup/_swap.swap ; elif [[ ! $(dmesg|grep _swap.swap) = *swap\ on* ]]; then swapon /backup/_swap.swap; fi
But it is necessary to make an explicit clarification that for this it is necessary to use an UNOFFICIAL add-on. Having an official add-on with the exact same name makes the road to victory a long one.
Is it possible to make the SWAP section 1Gb in size. Replacing “2G” with “1G” does not change anything.
Of course it works and I will make it clearer that the UNOFFICIAL addon is required in the OP.
My guess as to why 2G vs. 1G is not working is because the file already exists. So to do 1G, just remove the command from the addon config. Then take a reboot, open up SSH/terminal in any way you want. Go to the folder and DELETE the file. Then go back to the addon config and add the command back with 1G like you want followed by a reboot or a restart of the addon.
on my Raspberry Pi 4 with boot from microSD and data on external hard drive, for a few days the system restarts itself two or three times a day
-now implementing the recommended change, the database has increased me by 2 GB, but the home assistant system has not restarted by itself for a couple of days now !
I had this same issue after moving to an SSD drive. (Boot files on SD card).
I couldn’t get it to “auto apply” with the init[] commands in the SSH &Web terminal add-on but I SSHd to the Pi and ran them manually. Seems to have worked and now have a 2GB swap file/partition.
Checked backup directory for 2GB _swap.swap file creation.
.
-rw-r–r-- 1 root root 317460480 Dec 29 20:07 9b635441.tar
-rw------- 1 root root 2147483648 Jan 5 17:12 _swap.swap
-rw-r–r-- 1 root root 390932480 Dec 31 04:12 db0d8ce8.tar
.
Checked Supervisor log:
[18:26:09] INFO: Starting the ttyd daemon…
[18:26:10] INFO: Starting the SSH daemon…
Server listening on 0.0.0.0 port 22222.
Server listening on :: port 22222.
[2022/01/05 18:26:11:4225] N: Using foreign event loop…
Connection from 10.1.1.41 port 51763 on 10.1.1.111 port 22222 rdomain “”
.
.
I think it’s worked well. I’ll monitor memory allocation, stability and performance for any kinks.
One last question - should Protection mode be left disabled for future system reboots?
Thanks JZhass,
I have a daily restart automation set up on my HA. The SSH & Web Terminal init_command logic ran without issue. Also HA system appears much more stable with memory allocation generally less than 80% and the 2GB swap file working as hoped with around 80% of headroom. UI performance across the board appears much better with history graphs and other monitors loading within a couple of seconds like the HA of old. Will continue to monitor for a week or so and report any issues. Hope the SD card survives till I migrate to a USB portable drive.
Cheers,
Phil.
This worked great, until the 10.0 Update for the SSH addon. Now I get swapon: /backup/_swap.swap: Resource busy (which should be fine), followed by FATAL: Failed executing init command: (your command here) and that blocks the whole SSH addon from starting
EDIT — I’m on v10 and it still works for me. The busy resource piece means it’s still being used so it trips the entire command. I used to have swapoff then swapon there but that command is friggin broken in busybox. So reboot is likely your answer.