bash 如何向 OSX launchd plist 添加参数以将输出通过管道传输到日志文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13207365/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:41:46  来源:igfitidea点击:

How can I add arguments to OSX launchd plist to pipe output to a log file?

macosbashscriptinglaunchd

提问by ilium007

I have tried many different variation on this but just can't get it to work.

我已经尝试了许多不同的变体,但无法让它发挥作用。

I have a plist file:

我有一个 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>com.ilium007.handbrake</string>
        <key>ProgramArguments</key>
        <array>
                <string>/bin/bash</string>
                <string>/Users/ilium007/support/scripts/handbrake/handbrake.sh &gt;&gt; /Users/ilium007/support/scripts/handbrake/logs/handbrake_encode.log</string>
                <!--<string>&gt;&gt;</string>-->
                <!--<string>/Users/ilium007/support/scripts/handbrake/logs/handbrake_encode.log</string>-->
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>60</integer>
</dict>
</plist>

Note the comments in there.

注意里面的评论。

I want to pipe output from the handbrake.sh script to a log file and have tried to do so but keep getting A "no such file" error in system.log on OSX.

我想将 handbrake.sh 脚本的输出通过管道传输到日志文件,并尝试这样做,但在 OSX 上的 system.log 中不断收到“没有这样的文件”错误。

Nov  3 18:03:54 macmini com.ilium007.handbrake[15348]: /bin/bash: /Users/ilium007/support/scripts/handbrake/handbrake.sh >> /Users/ilium007/support/scripts/handbrake/logs/handbrake_encode.log: No such file or directory Nov  3 18:03:54 macmini com.apple.launchd.peruser.501[162] (com.ilium007.handbrake[15348]): Exited with code: 127

If I change the plist to the following, it runs but I get no log so I assume it is only running the first argument:

如果我将 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>com.ilium007.handbrake</string>
        <key>ProgramArguments</key>
        <array>
                <string>/bin/bash</string>
                <string>/Users/ilium007/support/scripts/handbrake/handbrake.sh</string>
                <string>&gt;&gt;</string>
                <string>/Users/ilium007/support/scripts/handbrake/logs/handbrake_encode.log</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>60</integer>
</dict>
</plist>

The command that I would like to run via launchd is:

我想通过 launchd 运行的命令是:

/Users/ilium007/support/scripts/handbrake/handbrake.sh >> /Users/ilium007/support/scripts/handbrake/logs/handbrake_encode.log

Nothing ever appears in the log file. But if I run that command from the terminal I get content in the log. Any help would be appreciated.

日志文件中从未出现任何内容。但是,如果我从终端运行该命令,则会在日志中获取内容。任何帮助,将不胜感激。

回答by Gordon Davisson

>>is a shell operator, not a program argument, and launchd doesn't pass its commands through a shell (unless you force it, as in @Lauri's answer). But launchd does have its own option for redirecting stdout:

>>是 shell 运算符,而不是程序参数,并且 launchd 不会通过 shell 传递它的命令(除非你强制它,如@Lauri 的回答)。但是 launchd 确实有自己的重定向标准输出选项:

<key>StandardOutPath</key>
<string>/Users/ilium007/support/scripts/handbrake/logs/handbrake_encode.log</string>

Note that it automatically appends (equivalent to >>, not >). You can also redirect stderr with the StandardErrorPathkey. One thing you cannot do is use ~(as in ~/support/scripts/handbrake/logs/handbrake_encode.log) -- that, again, is a shell feature that launchd doesn't emulate.

请注意,它会自动附加(相当于>>,而不是>)。您还可以使用StandardErrorPath密钥重定向 stderr 。你不能做的一件事是使用~(如在 ~/support/scripts/handbrake/logs/handbrake_encode.log 中)——同样,这是一个 launchd 无法模拟的 shell 功能。

回答by D.A. Reyn

The standard out and error keys are very useful, I redirected mine to the first terminal/shell as follows:

标准输出和错误键非常有用,我将我的重定向到第一个终端/shell,如下所示:

<key>StandardOutPath</key>
<string>/dev/ttys000</string>
<key>StandardErrorPath</key>
<string>/dev/ttys000</string>

You can change ttys000 to another number (ttys001..2) if you have multiple terminals. You can find the terminal id by typing in the shell:

如果您有多个终端,您可以将 ttys000 更改为另一个数字 (ttys001..2)。您可以通过在 shell 中键入来找到终端 ID:

tty

This helped me to find out why I was getting console errors in OS X, I needed to have the full path for every command and full paths for directories I was CD'ing to in scripts.

这帮助我找出为什么我在 OS X 中遇到控制台错误,我需要有每个命令的完整路径和我在脚本中 CD 到的目录的完整路径。

for instance grep is:

例如 grep 是:

/usr/bin/grep

and commands installed by macports use other paths also, like:

macports 安装的命令也使用其他路径,例如:

/opt/local/bin/lynx

I also tried to chown root:wheel the plist, but this doesn't seem to be necessary, but probably not a bad idea to prevent someone from editing it to whatever...

我也尝试 chown root:wheel the plist,但这似乎不是必需的,但可能不是一个坏主意,以防止有人将其编辑为任何......

edit:changed mine back to normal user after I got some permission errors which turned out to be because the script was trying to make files in / without permission, needed to add full paths to output files too, which is weird as it worked the previous day.

编辑:在我遇到一些权限错误后将我的改回普通用户,结果证明是因为脚本试图在未经许可的情况下在 / 中创建文件,还需要添加输出文件的完整路径,这很奇怪,因为它在以前的工作中起作用日。

回答by Lri

The redirection operators can't be used as arguments. You can either make another script for the pipeline or use bash -c.

重定向运算符不能用作参数。您可以为管道制作另一个脚本或使用bash -c.

<string>bash</string>
<string>-c</string>
<string>bash ~/Desktop/test.sh >> ~/Desktop/test</string>