OSX创建用于bash脚本执行的启动服务
我已经编写了一个bash脚本来持续检查服务。该脚本有一些函数,还调用其他源bash文件,在手动执行时工作正常。脚本有一个连续循环语句,类似于OSX创建用于bash脚本执行的启动服务,bash,macos,startup,launchd,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!"
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
/库/启动守护进程/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>
我面临的问题:
脚本的运行方式与手动执行时的运行方式不完全相同。我可以说launchd触发脚本,因为我可以从plist StandardOutPath日志文件/var/log/my service.log
中看到“是的,它正在运行”和“它没有运行!”消息
这里有人能帮我成功地将bash脚本作为服务运行吗?。与debian/centos不同,osx对我来说似乎很难。顺便说一句,是osx EI队长和塞拉版 以下内容适用于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>
残废
环境变量
路径
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
持久连接
成功退出
标签
这里的某个人
程序参数
来创建它。您也可以使用免费试用来解决当前的问题
您需要使用版本4:
需要macOS 10.11或更高版本(与macOS 10.12完美配合
塞拉)
以下内容适用于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>
残废
环境变量
路径
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
持久连接
成功退出
标签
这里的某个人
程序参数
来创建它。您也可以使用免费试用来解决当前的问题
您需要使用版本4:
需要macOS 10.11或更高版本(与macOS 10.12完美配合
塞拉)
这是脚本中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
您可能还希望访问以自动查看脚本。(虽然它没有检查缩进,这纯粹是为了人类的易读性。)这是对脚本中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
您可能还希望访问以自动查看脚本。(虽然它不检查缩进,这纯粹是为了人类的易读性。)试着在脚本的开头放一个合适的shebang/bin/bash
并从参数数组中删除bash
。另外,尝试缩进shell脚本,使其易读,并在脚本文件has中运行它/bin/bash
位于顶部以及它调用的所有其他源文件中。我可以删除plist中的参数。如果服务正在运行或未完美运行,但在服务正在运行或未运行时未执行某些操作,则会发出回声。我一直在测试这个脚本文件,它的工作方式正是我想要的。它在某个地方使用了sudo
来提示输入密码,我认为launchd不会在以root身份运行时等待回答,是吗?。请尝试在脚本的开头添加一个适当的shebang#/bin/bash
并从参数数组中删除bash
。另外,尝试缩进shell脚本,使其易读,并在脚本文件has中运行它/bin/bash
位于顶部以及它调用的所有其他源文件中。我可以删除plist中的参数。如果服务正在运行或未完美运行,但在服务正在运行或未运行时未执行某些操作,则会发出回声。我一直在测试这个脚本文件,它的工作方式正是我想要的。它使用了sudo
提示输入密码的地方,我认为launchd不会在以root身份运行时等待回答,是吗?哇,它现在可以工作了,可能环境变量就是关键。但是,launchctl list | stop | start
不起作用,知道为什么吗?嘿,伙计,我会看看我是否可以启动和停止这项服务,这本是一项伟大的工作,但仍然是一项伟大的工作。这对我帮助很大!。哇,它现在工作了,可能环境变量是关键。但是,launchctl list | stop | start
不起作用,知道为什么吗?嘿,伙计,我会看看我是否可以启动和停止这项服务,这本是一项伟大的工作,但仍然是一项伟大的工作。这对我帮助很大!。这出现在一个要删除的评论队列中,我觉得有点奇怪。我认为这是一个XY答案。这看起来是解决他们不知道的OP问题的有益尝试。没什么不寻常的。这出现在一个要删除的评论队列中,我觉得有点奇怪。我认为这是一个XY答案。这看起来是解决他们不知道的OP问题的有益尝试。很正常。