bash LaunchD Plist 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12586824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LaunchD Plist not working
提问by Chuck
EDIT: It appears as if I am getting an error in the console com.apple.launchd: (com.xxxx.adbind[57]) Exited with code: 1
编辑:似乎我在控制台 com.apple.launchd 中收到错误:(com.xxxx.adbind[57]) 退出代码:1
What does that even mean?
那有什么意思?
Also; if I load the launchd plist file logged in by using the launchctl command it works fine!
还; 如果我加载了使用 launchctl 命令登录的 launchd plist 文件,它工作正常!
I'm driving myself crazy trying to figure out why my launchd is not working. I'm using this in Mountain Lion 10.8.2 When I manually start it using launchctl it says it's loaded, but the script is not running. The script when ran manually works fine as well. Perhaps it just needs a better set of eye's to look at what I'm doing.
我快把自己逼疯了,试图弄清楚为什么我的 launchd 不起作用。我在 Mountain Lion 10.8.2 中使用它 当我使用 launchctl 手动启动它时,它说它已加载,但脚本没有运行。手动运行时的脚本也能正常工作。也许它只是需要一双更好的眼睛来看看我在做什么。
First, I'll explain what it is I'm trying to accomplish. I'm having about 400 computers imaged off site. I need these computers bound to AD, which can't be done out of our network. I figured by having a launchd script run at startup to call a script, then have the script check to see if it's in the network before it runs I can have these computers bound at first startup in our network before an AD user logs in.
首先,我将解释我想要完成的任务。我有大约 400 台计算机的异地映像。我需要这些电脑绑定AD,我们的网络是做不到的。我想通过在启动时运行 launchd 脚本来调用脚本,然后让脚本在运行之前检查它是否在网络中,我可以在 AD 用户登录之前在我们的网络中首次启动时绑定这些计算机。
Here's my launchd, I'm putting it in /Library/launchDaemons
这是我的launchd,我把它放在/Library/launchDaemons
<?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.xxxx.adbind</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/adbind.bash</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local/bin</string>
</dict>
</plist>
I've tried using this with and without a WorkingDirectory key.
我试过在有和没有 WorkingDirectory 键的情况下使用它。
Here's my script, I'm putting my script in /usr/local/bin
这是我的脚本,我将脚本放在 /usr/local/bin
#!/bin/bash
computerid=`/usr/sbin/scutil --get LocalHostName`
# Standard parameters
domain="xxx.xxxx.edu" # fully qualified DNS name of Active Directory Domain
udn="xxxxxx" # username of a privileged network user
password="xxxxx" # password of a privileged network user
ou="OU=xxx,DC=xxx,DC=xxxx,DC=edu" # Distinguished name of container for the computer
# Advanced options
alldomains="enable" # 'enable' or 'disable' automatic multi-domain authentication
localhome="enable" # 'enable' or 'disable' force home directory to local drive
protocol="smb" # 'afp' or 'smb' change how home is mounted from server
mobile="enable" # 'enable' or 'disable' mobile account support for offline logon
mobileconfirm="disable" # 'enable' or 'disable' warn the user that a mobile acct will be created
useuncpath="enable" # 'enable' or 'disable' use AD SMBHome attribute to determine the home dir
user_shell="/bin/bash" # e.g., /bin/bash or "none"
preferred="-preferred xxx.xxxxx.edu" # Use the specified server for all Directory lookups and authentication
# (e.g. "-nopreferred" or "-preferred ad.server.edu")
admingroups="xxx\admins,xxx\teachers,xxx\ADManagement - Computers,xxx\employees" # These comma-separated AD groups may administer the machine (e.g. "" or "APPLE\mac admins")
# Login hook setting -- specify the path to a login hook that you want to run instead of this script
### End of configuration
## Wait until all network services are up.
ipconfig waitall
# Check to see if we're in the district
if ping -c 1 xxx.xxx.x.x
then
# Activate the AD plugin
defaults write /Library/Preferences/DirectoryService/DirectoryService "Active Directory" "Active"
plutil -convert xml1 /Library/Preferences/DirectoryService/DirectoryService.plist
sleep 5
# Remove computer from OU
dsconfigad -f -r -u xxxxxxx -p xxxxxx
sleep 5
# Bind to AD
dsconfigad -f -a $computerid -domain $domain -u $udn -p "$password" -ou "$ou"
# Configure advanced AD plugin options
if [ "$admingroups" = "" ]; then
dsconfigad -nogroups
else
dsconfigad -groups "$admingroups"
fi
dsconfigad -alldomains $alldomains -localhome $localhome -protocol $protocol \
-mobile $mobile -mobileconfirm $mobileconfirm -useuncpath $useuncpath \
-shell $user_shell $preferred
# Restart DirectoryService (necessary to reload AD plugin activation settings)
killall DirectoryService
# Add the AD node to the search path
if [ "$alldomains" = "enable" ]; then
csp="/Active Directory/All Domains"
else
csp="/Active Directory/$domain"
fi
# This works in a pinch if the above code does not
defaults write /Library/Preferences/DirectoryService/SearchNodeConfig "Search Node Custom Path Array" -array "/Active Directory/All Domains"
defaults write /Library/Preferences/DirectoryService/SearchNodeConfig "Search Policy" -int 4
defaults write /Library/Preferences/DirectoryService/ContactsNodeConfig "Search Node Custom Path Array" -array "/Active Directory/All Domains"
defaults write /Library/Preferences/DirectoryService/ContactsNodeConfig "Search Policy" -int 4
plutil -convert xml1 /Library/Preferences/DirectoryService/SearchNodeConfig.plist
## Remove the script and launchd job. Be sure to delete the script.
launchctl unload -w /Library/LaunchDaemons/com.xxxx.adbind.plist
rm /Library/LaunchDaemons/com.xxxx.adbind.plist
rm /usr/local/bin/adbind.bash
exit 0
else
echo "District not Available. Quitting"
exit 1
fi
Thanks for any help!
谢谢你的帮助!
采纳答案by Chuck
Well, I finally worked through my problem and have found a solution! I'm going to post the answer here in order to hopefully help someone else someday! It seems as if the launchd file wasn't running long enough to run my entire script. So I added the KeepAlive key to the plist. It looks like this now.
好吧,我终于解决了我的问题并找到了解决方案!我将在这里发布答案,希望有一天能帮助其他人!似乎 launchd 文件运行的时间不够长,无法运行我的整个脚本。所以我在 plist 中添加了 KeepAlive 键。现在看起来像这样。
<?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.xxxx.adbind</string>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/adbind.bash</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
I also had troubles with permissions and found that the "bin" folder I created and placed the script into wasn't owned by root. So I ran chown and chmod on my files and folders. Like this.
我也遇到了权限问题,发现我创建并放置脚本的“bin”文件夹不归 root 所有。所以我对我的文件和文件夹运行了 chown 和 chmod。像这样。
sudo chown root:wheel bin
sudo chown root:wheel adbind.bash
sudo chmod 755 adbind.bash
sudo chown root:wheel com.xxxx.adbind.plist
sudo chmod 755 com.xxxx.adbind.plist
回答by ilium007
Exit code 1 means the script exited with an error condition. If it exited with a 0 it would mean there were no errors.
退出代码 1 表示脚本退出时出现错误情况。如果它以 0 退出,则意味着没有错误。

