用于bash脚本执行的OSX创建启动服务 [英] OSX create launchd service for bash script execution
本文介绍了用于bash脚本执行的OSX创建启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经编写了一个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触发了脚本,因为我可以看到"是的,它正在运行"和"它没有运行!"来自plist StandardOutPath日志文件/var/log/my-service.log
的消息。
这里有人能帮我成功地将bash脚本作为服务运行吗?与Debian/Centos不同,我在OSX中看起来很困难。顺便说一句,这是OSX EI船长版和塞拉版。
推荐答案
这是对脚本中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/以自动检查您的脚本。(尽管它不检查缩进,这纯粹是为了便于人类阅读。)
这篇关于用于bash脚本执行的OSX创建启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文