MacOS – running a simple shell script on startup in Yosemite

macbook promacosNetwork

Regarding stackexchange post how to get shell scripts to run at startup on Yosemite

The offering here is interesting but all I want to do is set the ip address of one of the ethernet interfaces, en0 with ifconfig. I don't want to launch a deamon.

the command to run is just:

ifconfig en0  inet 192.168.1.23 netmask 255.255.255.0 

Of course the interface drivers have to be launched and the interfaces available for configuration.
This has to be run by root, other wise it has to be prefaced with sudo, which
has to have the credentials provided.

I have the root account activated on this system and am comfortable with and know most of the hazards.

But I don't want to do this manually every time the machine is booted or restarted.

Perhaps someone from Apple has a reference to a patch or info on why the
network preference operations for manual configuration of interfaces will not set them on mine.

I have been buying and using Macs since the late 1990's. This is one of only two out of nearly a dozen Macs I have owned that is not newly purchased from
a reputable dealer.

Apache server will also not launch with the web sharing option.

Perhaps the machine with the installation is too old? It is

Note: I BOUGHT THIS MACHINE USED FOR CASH
 Model Name:    MacBook Pro
 Model Identifier:  MacBookPro5,1
 Processor Name:    Intel Core 2 Duo
 Processor Speed:   2.4 GHz
 Number of Processors:  1
 Total Number of Cores: 2
 L2 Cache:  3 MB
 Memory:    4 GB
 Bus Speed: 1.07 GHz
 Boot ROM Version:  MBP51.007E.B06
 SMC Version (system):  1.33f8
 <sn deleted for this post>
  Hardware UUID:    63410FEB-9CFF-5C8C-A692-8733BBEE36C5
 Sudden Motion Sensor:
 State: Enabled

This does not tell me its actuall manufacture date.

Thank you for time and attention

Jeff

Best Answer

You could save your script whatever you want and call it from a .plist file saved on /Library/LaunchAgents/ to be launched at startup.

Let me explain how to do it with an example.

  • Create you script and save it on /Users/username/setip.sh.
  • Create a new file called com.username.setip.plist in /Library/LaunchAgents/. This file must have the following format:
<?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.username.setip</string>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/username/setip.sh</string>
  </array>

  <key>Nice</key>
  <integer>1</integer>

  <key>StartInterval</key>
  <integer>60</integer>

  <key>RunAtLoad</key>
  <true/>

  <key>StandardErrorPath</key>
  <string>/tmp/com.username.setip.err</string>

  <key>StandardOutPath</key>
  <string>/tmp/com.username.setip.out</string>
</dict>
</plist>

The file is pretty self-explanatory. It will launch the command /Users/username/setip.sh every 60 seconds, will be launched at load, will save errors on /tmp/com.username.setip.err and logs on /tmp/com.username.setip.out.

Restart, and the script will be executed.