macosapplescriptrestartsata

Need Script To Automatically Restart Mac Until Negotiated SATA Link Speed is SATA II


I've got a couple of late 2009 Mac Minis running Mavericks. I recently replaced their hard drives with SSDs. Everything seems to work fine, however, I don't always get SATA II speeds. The computer often negotiates down to SATA I speeds. If I restart the computer enough times, it will eventually register at SATA II speeds and I'm good to go.

I don't think there's an elegant solution to the problem, but is there a script available, or that someone could write, that on start-up would check my negotiated SATA link Speed, and if it's less than 3.0GBPS it would automatically restart the computer and loop until it reads out the appropriate speed?

Thanks! Any help would be appreciated


Solution

  • If I found myself in the situation you've described, and could not replace the hardware, I'd set up a bash script to check the SATA Negotiated Link Speed of the SSD and if it's less than 3, reboot until it's not less then 3.

    Here is an example of what I'd do:

    Notes:

    In Terminal, execute the following commands to setup the bash script and Launch Daemon that will check the SATA Negotiated Link Speed of the SSD:

    sudo mkdir -p /usr/local/bin
    sudo touch /usr/local/bin/snls
    sudo nano /usr/local/bin/snls
    

    In nano, either type or copy and paste the following:

    #!/bin/bash
    
    [[ $(system_profiler SPSerialATADataType | awk '/Negotiated Link Speed:/{print int($4)}') -lt 3 ]] && shutdown -r now
    

    Save the changes and exit nano by pressing the following key sequences:
    ControlX
    Y
    Enter

    Still in Terminal:

    Make snls executable, only to root:

    sudo chmod 744  /usr/local/bin/snls
    

    Create the Launch Daemon .plist file for snls:

    sudo touch /Library/LaunchDaemons/com.sata.snls.plist
    sudo nano /Library/LaunchDaemons/com.sata.snls.plist
    

    Copy and paste the following into nano:

    <?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>com.sata.nls.com</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/bin/snls</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
    </plist>
    

    Save the changes and exit nano by pressing the following key sequences:
    ControlX
    Y
    Enter

    Load the Launch Daemon:

    sudo launchctl load /Library/LaunchDaemons/com.sata.snls.plist
    

    With this set, each time you boot and the SATA Negotiated Link Speed of the SSD is less than 3, it's going to reboot until it's 3, however many times it takes.


    WARNING: Do not undertake this process unless you know how to boot to Recovery Mode and that Recovery Mode is working on your system, or have an alternate method to access and modify the filesystem on the SSD. Also, you are comfortable in Recovery Mode Terminal to navigate to either target file created and delete them so you can reboot normally if something is not working, e.g. stuck in an endless reboot loop, with this method on your system.

    Have a look at: About macOS Recovery

    Note that when booted to macOS Recovery and you start Terminal, it's not like when you open Terminal in a normal boot. You are not by default in your normal Home directory, and typing cd / doesn't take you to the root of the e.g. Macintosh HD, you'd be in the root of OS X Base System. Also, nano is not in the PATH used by Terminal in macOS Recovery, although it's available if you type the proper path filename.

    When you open Terminal under macOS Recovery the PWD is /private/var/root, so to get to e.g. Macintosh HD, you type: cd /Volumes/Macintosh\ HD

    Again, if you have an issue with this method, deleting either snsl or com.sata.snls.plist from macOS Recovery will allow the system to boot normally, whatever normally is for you.