Keep MacBook Pro running when lid is closed

Another dialog with ChatGPT:

How do I tell my MacBook Pro what to do when I close the lid?

You can configure what your MacBook Pro should do when you close the lid by following these steps:

  1. Click on the Apple menu () in the upper-left corner of your screen.
  2. Select “System Preferences” from the drop-down menu.
  3. Click on the “Energy Saver” icon.
  4. In the Energy Saver window, you should see a section called “Battery” and “Power Adapter”.
  5. Click on the “Battery” or “Power Adapter” tab, depending on your needs.
  6. From the drop-down menu next to “When I close the lid” option, choose the action you want your MacBook to take when you close the lid.

You can select one of the following options:

  • Sleep: This will put your MacBook to sleep when you close the lid.
  • Display sleep: This will turn off the display but keep the MacBook running.
  • Do nothing: This will keep your MacBook running as if the lid is still open.

Once you’ve made your selection, close the System Preferences window, and your MacBook Pro will now follow the setting you selected when you close the lid.

How do I tell macOS High Sierra what to do when I close the lid?

To configure what your MacBook should do when you close the lid on macOS High Sierra, you can follow these steps:

  1. Click on the Apple menu () in the upper-left corner of your screen.
  2. Select “System Preferences” from the drop-down menu.
  3. Click on the “Energy Saver” icon.
  4. In the Energy Saver window, you will see a section called “Battery” and “Power Adapter”.
  5. Click on the “Battery” or “Power Adapter” tab, depending on your needs.
  6. From the drop-down menu next to “When I close the lid” option, select the action you want your MacBook to take when you close the lid.You can choose one of the following options:
    • Sleep: This will put your MacBook to sleep when you close the lid.
    • Display sleep: This will turn off the display but keep the MacBook running.
    • Do nothing: This will keep your MacBook running as if the lid is still open.

    Once you’ve made your selection, close the Energy Saver window. Your MacBook will now follow the setting you selected when you close the lid.

Note that the steps to configure the lid close behavior may vary slightly depending on the version of macOS you are using, but they are similar for most versions.

There is no “When I close the lid” option.

If you are not able to find the “When I close the lid” option on your MacBook running macOS High Sierra, it’s possible that your MacBook is not compatible with this feature.

Alternatively, it’s also possible that the “When I close the lid” option is hidden, and you can access it by using the Terminal app. Here’s how you can do it:

  1. Open the Terminal app. You can find it in the Utilities folder in the Applications folder, or you can use Spotlight to search for it.
  2. Type the following command in the Terminal window:

    sudo nvram boot-args=”iog=0x0″

  3. Press Enter and then enter your administrator password when prompted.
  4. Restart your MacBook.
  5. After restarting, go to System Preferences > Energy Saver, and you should see the “When I close the lid” option.

Once you have made your selection, you can revert the changes by opening the Terminal app and running the following command:

sudo nvram -d boot-args

Then restart your MacBook to apply the changes.

What does the following do? sudo nvram boot-args=”iog=0x0″

The command “sudo nvram boot-args=”iog=0x0″” sets a specific parameter in the system’s NVRAM (Non-Volatile Random Access Memory) that tells macOS to enable the “When I close the lid” option in the Energy Saver preferences.

By default, on some Mac models, this option is not visible in the Energy Saver preferences because it’s disabled in the firmware. This command changes the firmware settings and enables the “When I close the lid” option in the Energy Saver preferences.

Specifically, the “iog=0x0” parameter tells macOS to ignore the sensor that detects when the lid is closed, allowing the system to keep running even when the lid is closed. This can be useful if you want to keep your MacBook running when you close the lid, for example, if you are using an external display and want to keep the MacBook’s lid closed to save desk space.

It’s important to note that changing the NVRAM settings can potentially cause issues with your system, so it’s recommended to follow the steps carefully and only make changes if you understand the consequences.

I get this error: nvram: Error setting variable – ‘boot-args’: (iokit/common) general error

If you encounter the “nvram: Error setting variable – ‘boot-args’: (iokit/common) general error” message when trying to run the command “sudo nvram boot-args=”iog=0x0″”, it’s possible that your system is not allowing you to change the NVRAM settings.

Here are some possible reasons for this error and solutions to fix it:

  1. System Integrity Protection (SIP) is enabled: If you are running macOS High Sierra or later, SIP is enabled by default and prevents modifications to the NVRAM settings. To disable SIP, you will need to restart your MacBook in Recovery mode (hold down Command + R during startup), launch Terminal from the Utilities menu, and enter the command “csrutil disable” in the Terminal window. After disabling SIP, you can try the nvram command again. However, it’s important to note that disabling SIP can make your system more vulnerable to security threats, so it’s recommended to re-enable it after you’re done with the NVRAM changes.
  2. Third-party software conflict: Some third-party software, such as anti-virus or system utilities, can conflict with the NVRAM settings and prevent changes. Try quitting these applications and running the nvram command again.
  3. Hardware issue: In some cases, the “general error” message can indicate a hardware issue with the system’s NVRAM or other components. If none of the above solutions work, you may need to contact Apple Support or take your MacBook to an Apple Authorized Service Provider for further diagnosis.

Waiting for VirtualBox guests to close before reboot or shutdown

See this and this. Basically:

vim /etc/init.d/virtualbox
chmod +x /etc/init.d/virtualbox
update-rc.d virtualbox defaults

The /etc/init.d/virtualbox script should look like this:

#!/bin/sh

# 2017-08-07 jj5 - SEE: How make Debian wait for all VirtualBox guests to
# stop during shutdown/reboot?: https://superuser.com/a/929292/615689

### BEGIN INIT INFO
# Provides:          virtualbox_start_and_stop
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: 
# Description:       Start virtualbox on boot, and shutdown safely on shutdown/reboot.
### END INIT INFO

case "$1" in
  start)
    echo "Starting Virtualbox "

    # Do whatever to start or resume your virtualbox instances.
    # Perhaps look for a txt file someplace with VMs that need to be
    # restarted or resumed... then start 'em.
    ;;
  stop)
    echo "Stopping Virtualbox"

    # Do something to either shutdown or savestate your virtualbox instances.
    # maybe also save the instances that should be resumed into a txt file
    # someplace for the start method above.

    # 2017-08-07 jj5 - SEE:
    # https://askubuntu.com/a/457564

    # 2017-08-07 jj5 - NOTE: You should make sure that ACPI Shutdown actually
    # shuts down the guest...
    # 2017-08-07 jj5 - SEE: Force Ubuntu Desktop to shutdown on power button:
    # https://www.progclub.org/blog/2017/08/07/force-ubuntu-desktop-to-shutdown-on-power-button-keypress-acpi-shutdown/

    while [ -n "$( sudo -u jj5 VBoxManage list runningvms )" ]; do

      sudo -u jj5 VBoxManage list runningvms \
        | sed -r 's/.*\{(.*)\}/\1/' \
        | xargs -L1 -I {} sudo -u jj5 VBoxManage controlvm {} acpipowerbutton;

      sleep 1;

    done;

    ;;
  *)
    echo "Usage: /etc/init.d/virtualbox {start|stop}"
    exit 1
    ;;
esac

exit 0

You may also need to Force Ubuntu Desktop to shutdown on power button keypress