如何将 nohup 输出重定向到指定文件?

How can I redirect nohup output to a specified file?

我在 SO 上找到的其他示例中尝试的所有内容似乎都不起作用。我正在尝试 运行 我的应用程序使用 nohup 但将应用程序的输出附加到文件中。

我尝试了以下一些方法。 None 其中似乎有效。

nohup dotnet application.dll &> out.log &
nohup dotnet application.dll > out.log 2>&1 &
nohup dotnet application.dll > /opt/out.log &

我总是收到类似

的东西
-bash: out.log: Permission denied

我已经尝试 运行使用 sudo 启动应用程序,但它似乎仍然无法正常工作。


nohup dotnet application.dll &

工作正常,但它将输出定向到其他目录,如 /home/ubuntu/nohup.out

我做错了什么?

nohup dotnet application.dll > out.log 2>&1 & 是正确的形式。

> out.log 将 STDOUT 重定向到文件 out.log.

2>&1 将 fd2 (STDERR) 重定向到 fd1 (STDOUT),fd1 (STDOUT) 已经重定向到文件 out.log.

您的问题在于文件权限(或 read-only 文件系统)。在你的命令前添加 sudo 不能解决这个问题,因为只有 nohup dotnet application.dllsudo 作为 root 执行,输出重定向是由 bash 以你的普通用户权限完成的。您可以通过使用 root 权限调用单独的 shell 来解决此问题:

sudo sh -c 'nohup dotnet application.dll > out.log 2>&1 &'