[英]OSX create launchd service for bash script execution
I have written a bash script to continuously checks a service. 我编写了一个bash脚本来连续检查服务。 The script has some functions and also calls other source bash files and works fine when executed manually.
该脚本具有一些功能,还可以调用其他源bash文件,并且在手动执行时可以正常工作。 The script has a continuous loop statement something like,
该脚本具有连续循环语句,例如,
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: /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: /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
The issue what i'm facing : The script is not running exactly how it runs when manually executed. 我面临的问题 :脚本未完全按照手动执行的方式运行。 I can say launchd triggers the script as I could see "yes, it is running" and "It is not running!"
我可以说launchd触发了脚本,因为我看到的是“是的,它正在运行”和“它没有在运行!” messages from the plist StandardOutPath log file
/var/log/my-service.log
. 来自plist StandardOutPath日志文件
/var/log/my-service.log
。
Can anyone help me here to successfully run the bash script as service?. 谁能在这里帮助我成功地将bash脚本作为服务运行? It looks difficult for me in osx unlike debian/centos.
在OSX中,与debian / centos不同,这对我来说似乎很困难。 BTW, it's osx EI Captain and Sierra editions.
顺便说一句,它是osx EI Captain和Sierra版本。
This is a stylistic fix for the bash
antipatterns in your script. 这是脚本中
bash
反模式的样式修复。 I would post it as a comment but comments cannot contain code formatting. 我会将其作为注释发布,但注释不能包含代码格式。
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
You might also want to visit http://shellcheck.net/ for an automated review of your scripts. 您可能还希望访问http://shellcheck.net/ ,以自动查看脚本。 (Though it doesn't examine indentation, which is purely for human legibility.)
(尽管它不检查缩进,这纯粹是为了人类可读性。)
The following works for me for a bash script on OSX 10.10: 以下内容适用于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>
I used Lingon to create it. 我用Lingon创建它。 You could use the free trial for it as well to solve your current issue.
您也可以使用免费试用版来解决当前问题。
You would need to use version 4: 您将需要使用版本4:
Requires macOS 10.11 or later ( works perfectly with macOS 10.12 Sierra )
需要macOS 10.11或更高版本( 与macOS 10.12 Sierra完美配合使用 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.