macos 非常简单的 Launchd plist 没有运行我的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1370901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VERY simple Launchd plist not running my script
提问by
I'm trying to figure out why my launchd script is not working. It is extremely simple, but I am new to the mac environment and trying to get accustomed. Here's my plist. I know ProgramArguments
is required, so I just put the script path in there.
我想弄清楚为什么我的启动脚本不起作用。这非常简单,但我是 mac 环境的新手并试图习惯。这是我的 plist。我知道ProgramArguments
是必需的,所以我只是把脚本路径放在那里。
<?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.tomcat.plist</string>
<key>ProgramArguments</key>
<array>
<string>/opt/apache-tomcat-5.5.27/bin/startup.sh</string>
</array>
<key>OnDemand</key>
<false/>
</dict>
</plist>
When I try to run launchctl load <name>
it seems to load properly (in that it doesn't give me any error messages), but the script doesn't seem to be executing, even on reboot.
当我尝试运行时,launchctl load <name>
它似乎加载正确(因为它没有给我任何错误消息),但脚本似乎没有执行,即使在重新启动时也是如此。
I've used all the examples I've found online and I can't figure out why this isn't running my script on start up.
我已经使用了我在网上找到的所有示例,但我无法弄清楚为什么这不在启动时运行我的脚本。
回答by
Just in case anyone else runs across this issue, and already has <key>RunAtLoad</key><true/>
in their plist, I want to provide some additional solutions.
以防万一其他人遇到这个问题,并且已经<key>RunAtLoad</key><true/>
在他们的 plist 中,我想提供一些额外的解决方案。
Double check permissions to make sure that your script is executable (look for an 'x'):
仔细检查权限以确保您的脚本可执行(查找“x”):
ls -l /opt/apache-tomcat-5.5.27/bin/startup.sh
Modify permissions if necessary:
必要时修改权限:
chmod +x /opt/apache-tomcat-5.5.27/bin/startup.sh
Also run the script directly first and make sure it works:
还要先直接运行脚本并确保它可以正常工作:
/opt/apache-tomcat-5.5.27/bin/startup.sh
If the script is executable, and runs fine directly, try tailing the system log to debug launchd:
如果脚本是可执行的,并且直接运行良好,请尝试拖尾系统日志以调试launchd:
sudo launchctl log level debug
tail -f /var/log/system.log
The -f
flag (basically) continually shows the end (latest entries) of the log. You can remove this flag to just print a snapshot of the end of the log. If you use this flag, you'll need to open a new terminal to run other commands. Press CTRL + C to end the tail session. For more information:
该-f
标志(基本上)持续显示日志的结尾(最新条目)。您可以删除此标志以仅打印日志末尾的快照。如果使用此标志,则需要打开一个新终端来运行其他命令。按 CTRL + C 结束尾部会话。想要查询更多的信息:
man tail
When you're finished debugging:
调试完成后:
sudo launchctl log level error
There are other log levels. For more information:
还有其他日志级别。想要查询更多的信息:
man launchctl
If you make any changes to the script or plist, make sure you reload the plist. For example:
如果对脚本或 plist 进行任何更改,请确保重新加载 plist。例如:
launchctl unload ~/Library/LaunchAgents/com.tomcat.plist
launchctl load ~/Library/LaunchAgents/com.tomcat.plist
If you ONLY made changes to the script and NOT the plist, you can just restart the plist:
如果您只更改了脚本而不是 plist,则可以重新启动 plist:
launchctl stop com.tomcat.plist
launchctl start com.tomcat.plist
If you add the following key-value to your plist:
如果将以下键值添加到 plist:
<key>KeepAlive</key>
<true/>
Then you can just run:
然后你可以运行:
launchctl stop com.tomcat.plist
And it will restart automatically.
并且它会自动重启。
If none of this helps, and you're specifically having problems with setting up Tomcat on OS X, this tutorialmight be of help.
如果这些都没有帮助,并且您在 OS X 上设置 Tomcat 时遇到了特别的问题,那么本教程可能会有所帮助。
回答by James Mead
To make your script run automatically when you call launchctl load, you need to add :-
要在调用 launchctl load 时自动运行脚本,您需要添加:-
<key>RunAtLoad</key>
<true/>
Alternatively you could use :-
或者,您可以使用:-
launchctl start com.tomcat.plist
回答by fakedad
Although I imagine most people will not have this problem, I figure it is worth putting here since I spent nearly two hours trying to figure out why launchd load
was not working despite returning a 0
exit code.
虽然我想大多数人不会launchd load
遇到这个问题,但我认为值得把它放在这里,因为我花了将近两个小时试图找出为什么尽管返回了0
退出代码但仍然无法工作。
The problem was simple. My plist
file had the wrong file extension (I had "plst
"), and launchctl
was silently refusing to load the file. Changing the extension to plist
resolved the issue.
问题很简单。我的plist
文件有错误的文件扩展名(我有“ plst
”),并且launchctl
默默地拒绝加载文件。更改扩展名以plist
解决问题。