Config: Run shell script on login

Sometimes, I got frustrated with the fact that my SSH keys were cleared from agents after rebooting. As a result, I had to manually add those keys to agents every time my computer restarted (although it’s not occasional)… It’s best to automate such tasks - I thought. So, I asked “how to run a shell script on login?”. And here are some solutions.

Option 1: Automator

Option 2: Daemons

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>Label</key>
		<string>com.thuyen.loginscript</string>
		<key>ProgramArguments</key>
		<array><string>/Users/thuyentrinh/scripts/on_login.sh</string></array>
		<key>RunAtLoad</key>
		<true/>
	</dict>
</plist>

🔖 Note: For some reasons, the plist does not work with the executable path like ~/scripts/on_login.sh (~ for HOME directory). Explicit path /Users/thuyentrinh/scripts/on_login.sh solves the problems… My MacOS is 10.13; maybe that matters 🙄. I did not test it in earlier MacOS versions.

launchctl load ~/Library/LaunchAgents/com.thuyen.loginscript.plist
launchctl list | grep loginscript

Reference

  1. Launch shell script silently at login
  2. Daemons and Services Programming Guide
  3. Stackoverflow thread: Running script upon login mac