OSX为bash脚本执行创建launchd服务

时间:2016-10-17 19:00:55

标签: bash macos startup launchd

我编写了一个bash脚本来持续检查服务。该脚本具有一些功能,并且还调用其他源bash文件,并在手动执行时正常工作。该脚本有一个连续的循环语句,如

while true; do
{

$path/is_it_running
if [ "$?" == "0" ]; then
{
echo "yes, it is running"
$path/test
if [ "$?" != "0" ]; then
fix_the_issues
fi
}

else 
echo "It is not running!"

fi
sleep 10
}
done

/Library/LaunchDaemons/my.own.service.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>my.own.service</string>
        <key>ProgramArguments</key>
        <array>
            <string>/bin/bash</string>
            <string>/path/to/bash/script</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
         <key>KeepAlive</key>
        <true/>
        <key>StandardOutPath</key>
        <string>/var/log/my-service.log</string>
    </dict>
</plist>

sudo launchctl load -w /Library/LaunchDaemons/my.own.service.plist

/var/log/system.log:

com.apple.xpc.launchd[1] (com.apple.quicklook[80608]): Endpoint has been activated through legacy launch(3) APIs. Please switch to XPC or bootstrap_check_in(): com.apple.quicklook

我面临的问题: 脚本未正确运行手动执行时的运行方式。我可以说launchd触发了我可以看到的脚本&#34;是的,它正在运行&#34;和#34;它没有运行!&#34;来自plist StandardOutPath日志文件/var/log/my-service.log的消息。

有人可以帮我在这里成功运行bash脚本作为服务吗?对于我来说,在osx中​​看起来很难,而不像debian / centos。顺便说一下,它是osx EI Captain和Sierra版本。

2 个答案:

答案 0 :(得分:2)

这是脚本中bash反模式的样式修复。我会将其作为评论发布,但评论不能包含代码格式。

while true; do
#{  no braces necessary here
# note indentation
    # Don't examine $? -- if already does that for you
    if $path/is_it_running; then
    #{ no braces necessary here
    # note indentation
        echo "yes, it is running"
        # Don't examine $? -- if already does that for you
        if $path/test; then
            fix_the_issues
        fi

    #}  No braces necessary       
    else 
        # Note indentation
        echo "It is not running!"
    fi
    sleep 10
#} No braces necessarsy
done

您可能还想访问http://shellcheck.net/以自动审核您的脚本。 (虽然它没有检查缩进,这纯粹是为了人类的易读性。)

答案 1 :(得分:1)

以下适用于OSX 10.10上的bash脚本:

<?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>Disabled</key>
    <false/>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin</string>
    </dict>
    <key>KeepAlive</key>
    <dict>
         <key>SuccessfulExit</key>
         <false/>
     </dict>
    <key>Label</key>
    <string>SomeNameHere</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/path/to/bash/script</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

我使用 Lingon 来创建它。您也可以使用免费试用版来解决当前问题。

您需要使用版本4:

  

需要macOS 10.11或更高版本(与macOS 10.12完美配合   塞拉