wenlong1423
2/6/2015 - 1:56 PM

launchd 脚本(.plist) 中作为 root 执行任意 shell script.md

/Library/LaunchDaemons 中创建 ip-up.plist 文件,内容如下

<?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.wenlong.setchnroute.plist</string>
   <key>ProgramArguments</key>
   <array>
       <string>/Users/name/ip-up</string>
   </array>
   <key>KeepAlive</key>
   <false/>
   <key>RunAtLoad</key>
   <true/>
</dict>
</plist>

然后

sudo chown root:wheel /Library/LaunchDaemons/ip-up.plist
sudo chmod 644 /Library/LaunchDaemons/ip-up.plist

/Users/name/ip-up 中随便写写,最后 chmod +x 就行

PS. ip-up 其实是那个 chnroute 项目生成的路由表命令,似乎要等到网络加载上了才行,所以总是只有最后的几千条有效,我在前面加了个 sleep 9 似乎就能正常工作了(最起码看起来是正常的),如果你知道应该怎么弄,请告诉我,谢谢了!