I'm attempting to create a shutdown script in macOS (10.11) - that is, a script that runs at shutdown, not a script that shut's down the Mac. The linux "rc" system is not present in macOS.
I've searched and searched for a solution, and this is the only one I've been able to find. It launches at boot with launchd, and both the startup and shutdown functions run when they should:
#!/bin/bash
function startup()
{
## commands to create and fill ram disk
tail -f /dev/null &
wait $!
}
function shutdown()
{
## commands to backup contents of ramdisk
exit 0
}
trap shutdown SIGTERM
startup;
As I said, both the startup() and the shutdown() functions run when expected. The problem lies within the commands of the shutdown function. It's a pretty simple script, it just copies the contents of the ram disk to a folder on the hard drive:
function backup_ramdisk()
{
## empty ram disk backup folder
rm -R -f /webfolder-backup/*
## copy contents of the ramdisk to the ramdisk backup
cp -R /Volumes/webfolder/ /webfolder-backup/
## make me the owner
chown -R me /webfolder-backup/
chmod -R 777 /webfolder-backup/
exit 0
}
The actual script is loaded with extras that log stuff, and I can confirm that the entire script does run. But what happens is the cp
line fails. Sometimes it fails completely, and the backup folder is empty. But more often, it gets part of the structure of the RAM disk, and stops copying files right around (but not exactly) the same spot.
Often times it will throw the error cp: /Volumes/webfolder/: No such file or directory
even though it has already copied a hundred items from that folder. It makes me think that the shutdown command is unmounting the RAM disk before my script has time to finish backing it up. Keep in mind, because it's a RAM disk, it only takes a couple of seconds to copy all the files off of it. But it seems that's not enough. If there were a way I could pause the shutdown process while my script is running, then proceed, that would be ideal! Or maybe a better approach to this entirely?
So what I need is some way to potentially force the RAM disk to stay up for another few seconds, or perhaps to "pause" the shutdown process while this script is running.
– l008com Mar 11 '18 at 10:51tail -f /dev/null &
totail -f /Volumes/webfolder/.handle
and of course, created an invisible file called .handle on my RAM disk. The idea being that tail opening a file on the RAM disk will keep it open, and keep the ram disk un-ejectable until the script exits. It doesn't work... but it helps. I'm consistently getting MORE files copied with thecp
line. But not all of them. So I may be headed in the right direction, I just need to force the RAM disk to stay open for a few more seconds. – l008com Mar 11 '18 at 15:19