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
- Open Automator. Choose Application from the templates.
- Choose Run Shell Script from the action list.
- Paste your code there (see the figure below). For convenience, I launch another script which I could easily open and edit.
- Save the application.
- Open System Preferences -> Users & Groups -> Login Items. Then, add the application you have just created.
Option 2: Daemons
- Create a plist file in
~/Library/LaunchAgents
(ex.com.thuyen.loginscript.plist
):
<?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.
- Load the daemon:
launchctl load ~/Library/LaunchAgents/com.thuyen.loginscript.plist
- You could verify if the daemon is loaded by listing out services
launchctl list | grep loginscript