Running script upon login mac [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 7 years ago .
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.
3 Answers 3
- start Automator.app
- select Application
- click Show library in the toolbar (if hidden)
- add Run shell script (from the Actions/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 an your_name.app )
go to System Preferences -> Accounts -> Login items
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
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):
Replace the 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).
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.
Adding Startup Scripts to Launch Daemon on Mac OS X Sierra 10.12.6
Being new to working on OS X, it has been somewhat confusing to how things work on Mac compared to that on Linux — to be completely honest it has been a painstaking journey while wrapping my head around the differences between the systems.
I intend to share my experience relating to adding start up shell scripts to the Launch Daemon on OS X Sierra. Any suggestions for a better solution is most welcome.
A good starting point to get to know about Launch Daemon Process — how to configure, troubleshoot and get some examples can be found here.
A daemon is a program running in the background without requiring user input.
Task in Hand
- Create an shell script file to start mariadb (for simplicity of demo)
- Launch the shell script file as soon as the computer boots up (i.e. without requiring the user to login)
Step 1: Create an shell script file using the terminal
Now add in the following code to the file — save and exit. This primarily checks if the mariadb service is enabled or not and then starts the service if it is stopped.
Give the script file execution permission
Step 2: Launch the shell script file as soon as the computer boots up
We now have to add the script file to the Launch Daemon process — which will run the Job as soon as the Mac is booted.
The behavior of a daemon/agent is specified in a special XML file called a property list. Depending on where it is stored it will be treated as a daemon or an agent.
Again, just to be clear between the difference between daemon/agents:
The main difference is that an agent is run on behalf of the logged in user while a daemon runs on behalf of the root user or any user you specify with the UserName key.
In Mac OS X, the Launch Daemons are stored inside
folder while the Launch Agents are inside
Now, lets create a property list file(.plist) here as root user. (Please note there are conventions in naming the plist file which is outside the scope of this article)
Configuration of the plist file will vary upon your use case. The list of properties that can be configured is again nicely illustrated under the Configuration section on launchd.info
For easier illustration, please check the following plit file and the inline commets:
Now that we have all the necessary files ready and in place, we need to add the property list file onto the launchctl as follows:
- To permanently remove the plist to the Launch Daemon
- Once a script is added onto the Launch Daemon it will auto-start even if the user runs the following command
- A Gist with the source files is provided here.
Do let me know if this works for you! Any suggestion for improvement will be most appreciated.
Question: Q: Running script automatically at startup at system level
I am a newbie with MAC. I am looking for some application that is equivalent to Window’s Task Scheduler – bascially, I have a bash script and I would like it to start running at startup and be always running in the background, no matter who logs into the computer, and whoever logged in can restrat the script if needed.
Background info (if that helps understand what I am trying ot accomplish):
I am using Selenium (a UI automation tool) to automate some web testing, and my script will be calling the Selenium library to start a server to listen for requests, and it will launch browser and run tests when a request comes in.
Long story short about what I want:
I would like to automate opening up a terminal and running a bash script when my MAC starts up, and have the script always running in the background regardless of which user is currently logged in, and I would like the user to be able to stop the script and restart it if it somehow gets stuck.
I did some quick research and found out about Automator. I would like to find out/confirm if Automator is what and all I need? If not, what are my options out there?
Any suggestion and advice would be greatly appreciated! Thank you so much for reading!
Make shell script run at startup using terminal? (Mac OS X)
I know that you can use Automator to create an app out of the script that you can apply to the login files from the preferences. But would really love to know if you could make shell scripts run at startup only using terminal to set this up? Without having to move the mouse. Btw. The script will start different services that I am using for web development 🙂
1 Answer 1
To run scripts/commands you can use launchd .
You need to have 2 files specifically.
1) Your shell scripts.
Here is the sample plist: Save it as com.example.exampld.plist . Label and plist names are preferred to be given same by apple.
And place it in the according to your need.
Here are the folders:
According to your need place it either in 1st or 2nd folder from above list.
To run the script either load it using launchctl or restart the mac.
Loading and unloading the script:
Check out apple website for keys which can be used in plist.
How can I write a startup script for Mac OS X?
I have created a jar file in java, and I want to make the application automatically start during system boot up. I found I have to write a shell script for that. What should be that shell script look like? Is there anything else I have to do to make an application automatically start at boot up?
3 Answers 3
The preferred way to launch programs at OS X startup is to create a launchd daemon as explained here.
In mac os x you can also just run “open program.jar” (or any folder, ‘-a’ for native applications) and it will open detached from the terminal with any default environment settings that it would use if you just double clicked it.
You can also add a program (or the script) to the user’s login items through System Preferences > Accounts > ‘username’ > Login items. This is completely point and click and doesn’t require a shell script
The shellscript would be something like this:
Or maybe you don’t actually need to write Jar.jar, only Jar. I don’t quite remember.
Not the answer you’re looking for? Browse other questions tagged macos shell startup or ask your own question.
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2020.9.18.37632