5

Actually I want to make a RAM drive related to this script on my Mac.

Here is what I did:

  1. Disabled SIP in recovery mode using command "csrutil"
  2. Created a plist file in /Library/LaunchDaemons/
    and entered launchctl -w /Library/LaunchDaemons/vn.magik.ramdisk.plist

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
        <dict>
            <key>Label</key>
                <string>vn.magik.ramdisk</string>
            <key>ProgramArguments</key>
                <array>
                  <string>/Users/khacpm/ramdisk.sh</string>
                </array>
            <key>RunAtLoad</key>
                <true/>
            <key>StandardOutPath</key>
                <string>/Users/khacpm/ramdisk.log</string>
            <key>StandardErrorPath</key>
                <string>/Users/khacpm/ramdisk.err</string>
        </dict>
    </plist>
    
  3. Created .sh file and put it in ~/ramdisk.sh

    code:

    function fstartup()
    {
        //put folder into ramdisk
    }
    function fshutdown()
    {
        //detach ramdisk
    }
    
    function framdisk()
    {
        //do something
    }
    trap fshutdown SIGTERM
    trap fshutdown SIGKILL
    fstartup;
    

The problem is, the function fstartup runs well but it seems like function fshutdown isn't called by the system.

5
  • Have you tried disabling SIP to see if your scripts are even compatible with that new protection?
    – bmike
    Commented Oct 3, 2015 at 13:06
  • I set the nvram rootless=0 already. but it not work
    – Anh Bảy
    Commented Oct 3, 2015 at 14:00
  • You can't modify /System with sip. I'll answer how to disable that
    – bmike
    Commented Oct 3, 2015 at 14:03
  • Thanks god, I boot into recovery mode and disable SIP with command "csrutil disable" and now I can modify /System folder. But sadly what I want still not work, the script not run on startup and shutdown
    – Anh Bảy
    Commented Oct 3, 2015 at 14:15
  • Many years laster and I'm trying to do the same exact thing with the same exact starting script. I can get both the startup function and shutdown function to run, however the shutdown function which copies the files off the RAM disk will not complete. It seems the RAM disk is destroyed by the system before this very fast cp command can finish running. Is there any way to hold up the shutdown for a few seconds?
    – l008com
    Commented Mar 10, 2018 at 12:22

2 Answers 2

1

I believe the correction to the shell script should be:

function fstartup()
{
    //put folder into ramdisk
     tail -f /dev/null &
     wait $!
}
function fshutdown()
{
    //detach ramdisk

}

function framdisk()
{
    //do something
}
trap fshutdown SIGTERM
trap fshutdown SIGKILL
fstartup;

So that it will wait during the startup, not the shutdown.

I'm currently running my version and it appears to work fine, it correctly runs the shutdown function when the computer is shutting down.

0

I found one bug in my script: Missing these lines in function fshutdown:

tail -f /dev/null &
wait $!

the correct function is:

function fstartup()
{
    //put folder into ramdisk
}
function fshutdown()
{
    //detach ramdisk
     tail -f /dev/null &
     wait $!
}

function framdisk()
{
    //do something
}
trap fshutdown SIGTERM
trap fshutdown SIGKILL
fstartup;

base on apple document, When the system shuts down, it sends a SIGTERM signal to all of the daemons that it started.
But my old script will execute and close itself after it started.
You need to make your script alive and wait for Sigterm signal by adding some functions like "sleep" or "wait" to make the script alive.

1
  • 1
    If the removal of the bug solved your problem, please add an explication whether and why this solved your question in your answer.
    – klanomath
    Commented Oct 6, 2015 at 7:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .