I am wondering if anyone is able to help me out with getting a .sh file to run when 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.
People are saying to add it to the login shell file, but I do not know where that is. Some help please.
You can:
Save somewhere, for example you can make an "Applications" folder in your HOME (you will get an your_name.app)
Go to System Preferences -> Accounts -> Login items
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...
Create your shell script 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:
If multiple commands are needed something like this can be used:
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..plist
file according to the instructions in the Apple Dev docs here or more detail below.~/Library/LaunchAgents
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.
Replace the
<string>
after the Program key with your desired command.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, runlaunchctl 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.