preloader

How to run a command in MAC at BOOT time

blog-image

To run a command on your Mac at startup, you can use the launchd system process. Here are the steps:

  1. Open Terminal and type the following command to create a new launchd configuration file for your script:
sudo nano /Library/LaunchAgents/com.yourdomain.yourscript.plist

Note: Replace com.yourdomain.yourscript with the name you want to give your launchd configuration file, and replace /Library/LaunchAgents with the path where you want to store your configuration file.

  1. Add the following XML code to the file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Inc//DTD PLIST 1.0//EN" "http://www.apple.com/dtd/plist-1.0.dtd">
    <plist version="1.0">
        <machines>
            <machine>
                <systempath>/bin/yourscript</systempath>
            </machine>
        </machines>
        <keepalive>true</keepalive>
    </plist>
    

Note: Replace /bin/yourscript with the path to your script.

  1. Save the file by pressing Ctrl + X, then Y, and finally Return.
  2. Load the launchd configuration by running the following command in Terminal:
sudo launchctl load /Library/LaunchAgents/com.yourdomain.yourscript.plist

Note: Replace /Library/LaunchAgents/com.yourdomain.yourscript.plist with the path to your configuration file.

  1. Start the script by running the following command in Terminal:
sudo launchctl start com.yourdomain.yourscript

Note: Replace com.yourdomain.yourscript with the name you gave your launchd configuration file.

  1. To check the status of your script, run the following command in Terminal:
sudo launchctl list | grep yourscript

Note: Replace yourscript with the name of your script. This will display information about the script’s current state (running, stopped, etc.).

That’s it! Your script should now run automatically whenever you start your Mac.