user4479070
user4479070

Reputation:

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 :-)

Thanks!

Upvotes: 1

Views: 9746

Answers (1)

SkrewEverything
SkrewEverything

Reputation: 2523

To run scripts/commands you can use launchd.

You need to have 2 files specifically.

1) Your shell scripts.

2) plist file.

Here is the sample plist: Save it as com.example.exampld.plist. Label and plist names are preferred to be given same by apple.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.example.exampled</string>
    <key>LaunchOnlyOnce</key>
        <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/sh</string>
        <string>absolute_path_to_script</string>

    </array>
</dict>
</plist>

And place it in the according to your need.

Here are the folders:

|------------------|-----------------------------------|---------------------------------------------------|
| User Agents      | ~/Library/LaunchAgents            | Currently logged in user
|------------------|-----------------------------------|---------------------------------------------------|
| Global Agents    | /Library/LaunchAgents             | Currently logged in user
|------------------|-----------------------------------|---------------------------------------------------|
| Global Daemons   | /Library/LaunchDaemons            | root or the user specified with the key UserName
|------------------|-----------------------------------|---------------------------------------------------|
| System Agents    | /System/Library/LaunchAgents      | Currently logged in user
|------------------|-----------------------------------|---------------------------------------------------|
| System Daemons   | /System/Library/LaunchDaemons     | root or the user specified with the key UserName
|------------------|-----------------------------------|---------------------------------------------------|

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:

sudo launchctl load /Library/LaunchAgents/com.example.exampld.plist    // for loading
sudo launchctl unload /Library/LaunchAgents/com.example.exampld.plist  // for unloading

Check out apple website for keys which can be used in plist.

I hope it helps.

Upvotes: 3

Related Questions