112

I was wondering which file I should place this bash command in so it will be run on startup.

# Start the MongoDB server
/Applications/MongoDB/bin/mongod --dbpath /usr/local/mongo/data --fork --logpath /usr/local/mongo/log

I have been scouring the net and think it is between ~/.bashrc, ~/profile, /etc/bashrc, /etc/profile or ~/.bash_profile. Although I have tried these and they seem to run on terminal startup not Mac startup. Am I missing a file?

avpaderno
  • 1,490
Wolfy87
  • 1,223

12 Answers12

110

Another simple solution from Stack Overflow: You can:

  • Start Automator.app;
  • Select "Application";
  • Click "Show library" in the toolbar (if hidden);
  • Add "Run shell script" (from the Actions/Utilities);
  • Copy-and-paste your script into the window;
  • Test it;
  • Save it somewhere: a file called your_name.app will be created);
  • Depending your MacOSX version:
    • Old versions: Go to System Preferences → Accounts → Login items, or
    • Before Ventura version: Go to System Preferences → Users and Groups → Login items (top right), or
    • Ventura and after version: Go to System Preferences → General → Login items;
  • Add this newly-created app;

Log off, log back in, and you should be done. ;)

jsgoupil
  • 103
Jaime M.
  • 1,250
83

To run a command on start up on OS X, you need to use launchd.

If you don't want to use Lingon, you need to create a launchd Property List. This is an XML file, so you can do it with your favourite text editor or alternatively you can use the Property List Editor that's installed with the Mac OS X Dev Tools. Create the following:

<?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>some.meaningful.name</string> <!-- org.mongodb.mongodb perhaps? -->
&lt;key&gt;OnDemand&lt;/key&gt;
&lt;false/&gt;

&lt;key&gt;UserName&lt;/key&gt;
&lt;string&gt;anAppropriateUser&lt;/string&gt;

&lt;key&gt;GroupName&lt;/key&gt;
&lt;string&gt;anAppropriateGroup&lt;/string&gt;

&lt;key&gt;ProgramArguments&lt;/key&gt;
&lt;array&gt;
        &lt;string&gt;/Applications/MongoDB/bin/mongod&lt;/string&gt;
        &lt;string&gt;--dbpath&lt;/string&gt;
        &lt;string&gt;/usr/local/mongo/data&lt;/string&gt;
        &lt;string&gt;--fork&lt;/string&gt;
        &lt;string&gt;--logpath&lt;/string&gt;
        &lt;string&gt;/usr/local/mongo/log&lt;/string&gt;
&lt;/array&gt;

</dict> </plist>

Save this in /Library/LaunchAgents/some.meaningful.name.plist (you will need an administrator account and/or sudo), then open a terminal and do:

sudo launchctl load /Library/LaunchAgents/some.meaningful.name.plist

This will cause launchd to load the item which will cause it to start MongoDB on boot. As a bonus, launchd will monitor it and, if it exits for any reason, it will be re-started. To get rid of the item simply replace load in the above command with unload.

Y. E.
  • 105
Scott
  • 5,993
54

To launch commands when logging in, you can do this:

  • Create a text file containing your commands (bash script):

    #!/bin/bash
    
    # Start the MongoDB server
    /Applications/MongoDB/bin/mongod --dbpath /usr/local/mongo/data --fork --logpath /usr/local/mongo/log
    
  • Save this file in ~/Library/Startup.cmd

  • You can test it by double-clicking the file in the Finder
  • Make it executable: chmod +x ~/Library/Startup.cmd
  • Add this file in System Preferences > Accounts > Login items
Geekarist
  • 667
51

Officially none of these. The Apple suggested way is to use launchd. Guis to set this up include lingon and Launch Control

As for the files you mention the ones in the home directory ~/.bashrc, ~/profile, ~/.bash_profile are only started when you login via a terminal. The ones in /etc are run by the shell starting for all users before the ones in home directory but only when a user login is made.. bash manual

The Unix startup script involved /etc/rc* but for OSX just use the launchd stuff

mmmmmm
  • 6,242
14

If you want an approach that will work on Linux & Mac OS X, a cron task should should be sufficient (edit your cron tasks by executing crontab -e):

@reboot /path/to/script

(credits: https://unix.stackexchange.com/questions/49207/how-do-i-set-a-script-that-it-will-run-on-start-up-in-freebsd)

4

You will have to look at how launchd and launchctl work on MacOS. You could start by reading the man pages for both the commands. You could then look inside /Library/LaunchAgents/ and /Library/LaunchDaemons/ for examples of how to set up applications to load at different times through the launchctl interface.

Here's an example I found on Stack Overflow that might help you further.

ayaz
  • 11,970
1

Although I do not find the answers that are asking you to add the call in .bash_profile or .bashrc acceptable, they could work imho.

You could right click on your Terminal Icon when on Dock, Select Options > Open at login.

I have an auto-login VPN script for work. I need to open Terminal too when working. So I prefer it automatically launches at startup and I used .bashrc to run my vpn script automatically.

0

I was interested in a very simple unix answer to this problem and found it at another site. Here is a summary of the solution.

The standard for login shells is to always look for the bash configuration files with "profile" in the name, in this order: /etc/profile, ~/.bash_profile, then ~/.bash_login and lastly ~/.profile. When login shells exit, they read ~/.bash_logout.

In my case I just created the ~/.bash_profile and then I opened the preferences for the Mac Terminal app and changed the "Shell opens with" option from default to /bin/bash. That's it. Clean and simple.

0

You could use crontab:

First, create a script somewhere (let's say /path/to/script.sh) and type the command you wanted to run there. Then type chmod +x /path/to/script.sh.

Now, in the Terminal:

EDITOR=nano crontab -e

You will be presented with a text editor in the terminal.

Type in the file:

@reboot /path/to/script.sh

Hit Ctrl + X, hit y, and hit enter, and on the next boot, this will run!

If you wanted to run a command in the background, simply add an & after the command in the script.

DarkDiamond
  • 1,919
  • 11
  • 15
  • 21
0

This answer explains how to run a command on at login and is based on Geekarist's answer. Here, the script is wrapped in an AppleScript application. AppleScript was chosen over a workflow, because the Script Editor application does not overwrite manual changes made to an applications Info.plist file, where as the Automator application does. Wrapping the script in an application also allows the user to customize privacy settings. If the script exits with a non-zero status, then the application shows a popup window with an error message. Otherwise, no other windows are shown.

Steps are given below.

  1. Use the Script Editor application to create a application containing the line given below.

    do shell script quoted form of (POSIX path of (path to me) & "Contents/Resources/Startup.sh")
    
  2. Save the application to your ~/Applications folder. (If this folder does not exist, then create the folder.) Here, I will name the application "My Agent". You may choose a different name.

  3. Create a file named Startup.sh in My Agent application's Contents/Resources folder. Place the script in the file. A copy of the script from Geekarist's answer is given below.

    #!/bin/bash
    

    Start the MongoDB server

    /Applications/MongoDB/bin/mongod --dbpath /usr/local/mongo/data --fork --logpath /usr/local/mongo/log

  4. Enter the command given below to make Startup.sh executable.

    chmod +x ~/Applications/My\ Agent.app/Contents/Resources/Startup.sh
    
  5. Make the My Agent application an agent. This will prevent (amongst other things) the My Agent application from appearing on the dock while executing at startup.

    /usr/libexec/PlistBuddy -c "Add :LSUIElement bool true" ~/Applications/My\ Agent.app/Contents/Info.plist
    

    The above command adds the following lines to My Agent application's Info.plist file.

        <key>LSUIElement</key>
        <true/>
    
  6. Open the My Agent application in the Script Editor application. Compile the script, save and quit. This is necessary, because the Info.plist file was manually changed in the previous step.

  7. Add the My Agent application to the user login items.

-1

Open Terminal app select Terminal preferences go to profiles select Profiles Under Startup enter source path to script script Name

-5

open terminal, type

nano ~/.bash_profile

then add this text to the file:

/Applications/MongoDB/bin/mongod --dbpath /usr/local/mongo/data --fork logpath /usr/local/mongo/log
djsmiley2kStaysInside
  • 6,943
  • 2
  • 36
  • 48
MAX
  • 1