在运行时,如何将管道输出输出到文件?

问题描述 投票:0回答:2
文件:

ExecStart=/apppath/appname > /filepath/filename 2>&1

但这不起作用。输出最终以/var/log/log/消息最终使用,并且可以使用日记cttl查看,但我想要一个单独的文件。 我还尝试了设置
StdOutput=tty
,但找不到将其重定向到文件的方法。

任何帮助将不胜感激。

systemd.service(5)

说:
linux stdout fedora systemd stderr
2个回答
52
投票

ExecStart=

命令及其参数启动时执行。
So,

systemd
用args

/apppath/appname

>

/filepath/filename

运行
2>&1
try:
ExecStart=/bin/sh -c '/apppath/appname > /filepath/filename 2>&1'

try:
ExecStart=/usr/bin/sh -c "/apppath/appname > /filepath/filename 2>&1"

Execstart要求第一个参数是二进制论点(不例外),并且不允许管道或重定向。因此,使用execstart启动一个外壳,您可以在其中完成所有需要的花哨的事情。

man:systemd.exec

       StandardOutput=
           Controls where file descriptor 1 (stdout) of the executed processes is connected to. Takes one of inherit,
           null, tty, journal, kmsg, journal+console, kmsg+console, file:path, append:path, truncate:path, socket or
           fd:name.

           The file:path option may be used to connect a specific file system object to standard output. The semantics
           are similar to the same option of StandardInput=, see above. If path refers to a regular file on the
           filesystem, it is opened (created if it doesn't exist yet) for writing at the beginning of the file, but
           without truncating it. If standard input and output are directed to the same file path, it is opened only
           once — for reading as well as writing — and duplicated. This is particularly useful when the specified path
           refers to an AF_UNIX socket in the file system, as in that case only a single stream connection is created
           for both input and output.

           append:path is similar to file:path above, but it opens the file in append mode.


6
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.