bash - LaunchD Plist 不工作

标签 bash active-directory sh osx-mountain-lion launchd

编辑:似乎我在控制台中收到错误 com.apple.launchd:(com.xxxx.adbind[57])退出,代码:1

这是什么意思?

还有;如果我加载使用 launchctl 命令登录的 launchd plist 文件,它工作正常!

我快把自己逼疯了,试图找出为什么我的 launchd 无法工作。我在 Mountain Lion 10.8.2 中使用它,当我使用 launchctl 手动启动它时,它说它已加载,但脚本没有运行。手动运行时的脚本也可以正常工作。也许只是需要更好的眼光来看待我正在做的事情。

首先,我将解释我想要实现的目标。我对大约 400 台计算机进行了异地成像。我需要将这些计算机绑定(bind)到 AD,这无法在我们的网络之外完成。我想通过在启动时运行 launchd 脚本来调用脚本,然后让脚本在运行之前检查它是否在网络中,我可以在 AD 用户登录之前将这些计算机在首次启动时绑定(bind)在我们的网络中。

这是我的 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>

我尝试过使用和不使用WorkingDirectory键来使用它。

这是我的脚本,我将脚本放在/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

感谢您的帮助!

最佳答案

退出代码 1 表示脚本因错误情况退出。如果它以 0 退出,则意味着没有错误。

关于bash - LaunchD Plist 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12586824/

相关文章:

tomcat - 如何使用 Active Directory 对 Java Web 应用程序进行单点登录

bash:删除所有早于 1 个月的文件,但保留星期一的文件

linux - 不必记住我要 pull 和推的分支的最佳别名是什么?

bash - Awk 搜索并附加来自其他 csv 文件的匹配名称

ruby-on-rails - 使用 net-ldap 从 AD 组中删除成员

c# - 如何使用 C# 针对 Azure AD 验证用户密码

ruby - 通过参数将列表传递给 postgresql

linux - bash 脚本中无法访问系统环境变量

macos - LLDB - 退出...退出?

java - 如何将参数传递给命令行 Java 程序并转义所有特殊字符?