Mac OS X / Mac Apps 2025-02-16 07:50:41
Khalequzzaman

Published By

Khalequzzaman

Automatically Execute a Shell Script on Mac Startup

This tutorial will guide you through setting up a shell script (install.sh) to run automatically every time your Mac starts up. We'll use launchd, macOS's service management framework, to achieve this.

Prerequisites

  1. A shell script that you want to execute (e.g., /Users/khalequzzaman/PhpStorm/jetbra/scripts/install.sh).
  2. Basic familiarity with the Terminal.

Step 1: Verify the Shell Script

Before setting up the script to run automatically, ensure that it is executable and works as expected.
  1. Open Terminal.
  2. Navigate to the directory containing your script:
    cd /Users/khalequzzaman/PhpStorm/jetbra/scripts
  3. Make the script executable (if it isn't already):
    chmod +x install.sh
  4. Test the script manually to ensure it works:
    ./install.sh
    If the script runs successfully, you should see output like:
    done. the "kill Dock" command can fix the crash issue.

Step 2: Create a Launch Agent

Launch Agents are user-specific and run when the user logs in. We'll create a .plist file to define the Launch Agent.
  1. Open Terminal.
  2. Create a new .plist file in the ~/Library/LaunchAgents/ directory:
    nano ~/Library/LaunchAgents/com.khalequzzaman.installphpstorm.plist
  3. Add the following content to the file:
    <?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.khalequzzaman.installphpstorm</string>
    
        <key>ProgramArguments</key>
        <array>
            <string>/Users/khalequzzaman/PhpStorm/jetbra/scripts/install.sh</string>
        </array>
    
        <key>RunAtLoad</key>
        <true/>
    </dict>
    </plist>
    • Replace /Users/khalequzzaman/PhpStorm/jetbra/scripts/install.sh with the full path to your script.
    • The Label key is a unique identifier for your Launch Agent. You can name it anything, but it should be unique.
  4. Save the file and exit the editor:
    • In nano, press CTRL + X, then Y, and Enter to save.

Step 3: Load the Launch Agent

Load the Launch Agent to register it with launchd.
  1. In Terminal, run the following command:
    launchctl load ~/Library/LaunchAgents/com.khalequzzaman.installphpstorm.plist
    This ensures the Launch Agent is active and will run at startup.

Step 4: Test the Launch Agent

To verify that the Launch Agent works, you can either restart your Mac or manually trigger it.
  1. Manually start the Launch Agent:
    launchctl start com.khalequzzaman.installphpstorm
  2. Check the output to confirm that the script executed successfully. For example, if your script outputs:
    done. the "kill Dock" command can fix the crash issue.
    Then it is working as expected.

Step 5: Restart Your Mac

Restart your Mac to confirm that the script runs automatically at startup.
  1. Click the Apple menu and select Restart.
  2. After your Mac reboots, check the output of the script (if any) or verify that the intended actions were performed.

Step 6: Debugging (Optional)

If the script doesn't run as expected, you can check the system logs for errors.
  1. Open the Console app on your Mac.
  2. Search for com.khalequzzaman.installphpstorm or the name of your script to see any error messages.

Optional: Log Output to a File

If you want to log the output of the script for debugging purposes, modify the .plist file to include the following:
<key>StandardOutPath</key>
<string>/Users/khalequzzaman/PhpStorm/jetbra/scripts/install.log</string>
<key>StandardErrorPath</key>
<string>/Users/khalequzzaman/PhpStorm/jetbra/scripts/install-error.log</string>
This will save the output and errors to the specified log files.

Step 7: Update the Script (Optional)

If your script mentions a crash issue with the Dock (e.g., the "kill Dock" command can fix the crash issue), you can update the script to include the killall Dock command:
  1. Open the script in a text editor:
    nano /Users/khalequzzaman/PhpStorm/jetbra/scripts/install.sh
  2. Add the following line to the script:
    killall Dock
  3. Save and exit the editor.

Step 8: Unload or Remove the Launch Agent (Optional)

If you no longer want the script to run automatically, you can unload or remove the Launch Agent.
  1. Unload the agent:
    launchctl unload ~/Library/LaunchAgents/com.khalequzzaman.installphpstorm.plist
  2. Delete the .plist file:
    rm ~/Library/LaunchAgents/com.khalequzzaman.installphpstorm.plist

Conclusion

You've successfully set up a shell script to run automatically on Mac startup using launchd. This method is reliable and integrates well with macOS's service management framework.

Comments

No approved comments yet.

Add Comment