Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Improve this questionOriginal close reason(s) were not resolved
I am wondering if anyone is able to help me out with getting a shell (.sh) p开发者_Go百科rogram to automatically run whenever I log in to my account on my computer. I am running Mac OS X 10.6.7.
I have a file "Example.sh" that I want to run when I log onto my computer. I do not have a problem running it when I am already logged in, but I want this to run automatically.
Follow this:
start
Automator.app
select
Application
click
Show library
in the toolbar (if hidden)add
Run shell script
(from theActions/Utilities
)copy & paste your script into the window
test it
save somewhere (for example you can make an
Applications
folder in your HOME, you will get anyour_name.app
)go to
System Preferences
->Users & Groups
->Login items
(orSystem Preferences
->Accounts
->Login items
/ depending of your MacOS version)add this app
test & done ;)
EDIT:
I've recently earned a "Good answer" badge for this answer. While my solution is simple and working, the cleanest way to run any program or shell script at login time is described in @trisweb's answer, unless, you want interactivity.
With automator solution you can do things like next:
so, asking to run a script or quit the app, asking passwords, running other automator workflows at login time, conditionally run applications at login time and so on...
tl;dr: use OSX's native process launcher and manager, launchd
.
To do so, make a launchctl
daemon. You'll have full control over all aspects of the script. You can run once or keep alive as a daemon. In most cases, this is the way to go.
- Create a
.plist
file according to the instructions in the Apple Dev docs here or more detail below. - Place in
~/Library/LaunchAgents
- Log in (or run manually via
launchctl load [filename.plist]
)
For more on launchd
, the wikipedia article is quite good and describes the system and its advantages over other older systems.
Here's the specific plist file to run a script at login.
Updated 2017/09/25 for OSX El Capitan and newer (credit to José Messias Jr):
<?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.user.loginscript</string>
<key>ProgramArguments</key>
<array><string>/path/to/executable/script.sh</string></array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Replace the <string>
after the Program key with your desired command (note that any script referenced by that command must be executable: chmod a+x /path/to/executable/script.sh
to ensure it is for all users).
Save as ~/Library/LaunchAgents/com.user.loginscript.plist
Run launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist
and log out/in to test (or to test directly, run launchctl start com.user.loginscript
)
Tail /var/log/system.log
for error messages.
The key is that this is a User-specific launchd entry, so it will be run on login for the given user. System-specific launch daemons (placed in /Library/LaunchDaemons
) are run on boot.
If you want a script to run on login for all users, I believe LoginHook is your only option, and that's probably the reason it exists.
Create a shell script named as
login.sh
in your $HOME folder.Paste the following one-line script into Script Editor:
do shell script "$HOME/login.sh"
Then save it as an application.
Finally add the application to your login items.
If you want to make the script output visual, you can swap step 2 for this:
tell application "Terminal"
activate
do script "$HOME/login.sh"
end tell
If multiple commands are needed something like this can be used:
tell application "Terminal"
activate
do script "cd $HOME"
do script "./login.sh" in window 1
end tell
精彩评论