Mac OS X / Mac Apps
2025-02-16 07:50:41
Published By
KhalequzzamanAutomatically Execute a Shell Script on Mac Startup
This tutorial will guide you through setting up a shell script (
This will save the output and errors to the specified log files.
install.sh) to run automatically every time your Mac starts up. We'll use launchd, macOS's service management framework, to achieve this.
Prerequisites
- A shell script that you want to execute (e.g.,
/Users/khalequzzaman/PhpStorm/jetbra/scripts/install.sh). - 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.- Open Terminal.
- Navigate to the directory containing your script:
cd /Users/khalequzzaman/PhpStorm/jetbra/scripts - Make the script executable (if it isn't already):
chmod +x install.sh - Test the script manually to ensure it works:
If the script runs successfully, you should see output like:
./install.sh
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.
- Open Terminal.
- Create a new
.plistfile in the~/Library/LaunchAgents/directory:nano ~/Library/LaunchAgents/com.khalequzzaman.installphpstorm.plist - 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.shwith the full path to your script. - The
Labelkey is a unique identifier for your Launch Agent. You can name it anything, but it should be unique.
- Replace
- Save the file and exit the editor:
- In
nano, pressCTRL + X, thenY, andEnterto save.
- In
Step 3: Load the Launch Agent
Load the Launch Agent to register it withlaunchd.
- In Terminal, run the following command:
This ensures the Launch Agent is active and will run at startup.
launchctl load ~/Library/LaunchAgents/com.khalequzzaman.installphpstorm.plist
Step 4: Test the Launch Agent
To verify that the Launch Agent works, you can either restart your Mac or manually trigger it.- Manually start the Launch Agent:
launchctl start com.khalequzzaman.installphpstorm
- Check the output to confirm that the script executed successfully. For example, if your script outputs:
Then it is working as expected.
done. the "kill Dock" command can fix the crash issue.
Step 5: Restart Your Mac
Restart your Mac to confirm that the script runs automatically at startup.- Click the Apple menu and select Restart.
- 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.- Open the
Consoleapp on your Mac. - Search for
com.khalequzzaman.installphpstormor 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>
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:
- Open the script in a text editor:
nano /Users/khalequzzaman/PhpStorm/jetbra/scripts/install.sh - Add the following line to the script:
killall Dock - 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.- Unload the agent:
launchctl unload ~/Library/LaunchAgents/com.khalequzzaman.installphpstorm.plist
- Delete the
.plistfile:rm ~/Library/LaunchAgents/com.khalequzzaman.installphpstorm.plist
Conclusion
You've successfully set up a shell script to run automatically on Mac startup usinglaunchd. This method is reliable and integrates well with macOS's service management framework.