RAM Disk Ejects Too Fast on Shutdown
This is a more specific, more drilled-down version of a post I made a few weeks ago. The situation is this:
I have a shell script that runs on boot as a launchd that creates a RAM disk on my server, and copies a bunch of files to it. The shell script keeps running, and when the computer is shutdown, it runs it's own "shutdown" function that copies all of the files off of the RAM disk before the system shuts down.
Please note that this is not the only way I backup the data on this RAM disk. I am fully aware how tenuous RAM disks and the data kept on them are. The data is backed up in several other ways. But the final writing out on shutdown is an important one to keep recent changes from being lost.
Back to the script. It all works properly, the right parts of my script run when they should. The problem seems to be however, that the system sends my script the shutdown command, and at the same time, it shuts down the RAM disk system. Thus I am only able to copy a small amount of the files off before the RAM disk ceases to exist. It only takes a few seconds to run this copy command, but apparently you only get one second.
The script uses a `tail -f /dev/null` command to keep it running. I changed that to `tail -f /path/to/ramdisk/.handle`, the idea being it would keep a file open on the RAM disk and that would prevent the RAM disk from ejecting until after my script had finished coping the files. It *helped*, I get more of the files, but I don't get all of them.
So unless there's a better way entirely to do this, what it seems I need is a way to "jam" the RAM disk open, so it can't unmount, until my script is done. I do not have a lot of experience with anything beyond the most simple of shell scripts, so I'm in uncharted waters a bit.
This is the simple script I use. It works very well, and if I were doing anything other than access a RAM disk, it seems like it would work perfectly. In my destroy ramdisk function, I just need a way to prop the RAM disk open before I call the backup script, then remove the prop immediately after.
#!/bin/bash
function create_ramdisk()
{
# /create.ramdisk.sh
tail -f /Volumes/ramdisk/.handle &
wait $!
}
function destroy_ramdisk()
{
## /backup.ramdisk.sh
exit 0
}
trap destroy_ramdisk SIGTERM
create_ramdisk;
Xserve, OS X El Capitan (10.11.6), 2009