如何将nohup输出重定向到指定文件? [英] How can I redirect nohup output to a specified file?
问题描述
我在SO上发现的其他示例中尝试的所有内容似乎都不起作用.我试图使用nohup运行我的应用程序,但将应用程序的输出附加到文件中.
Everything I have tried from other examples I have found on SO don't seem to work. I am trying to run my application using nohup but append the output of the application to a file.
我尝试了以下一些方法.似乎都不起作用.
I have tried some of the following. None of which seem to work.
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运行该应用程序,但它似乎仍然无法正常工作.
I have tried running the application using sudo but it still doesn't seem to work.
nohup dotnet application.dll &
效果很好,但是它将输出定向到其他目录,例如/home/ubuntu/nohup.out
Works fine, but it directs the output to some other directory like /home/ubuntu/nohup.out
我在做什么错了?
推荐答案
nohup dotnet application.dll>out.log 2>& 1&
是正确的格式.
>out.log
将STDOUT重定向到文件 out.log
.
> out.log
redirects STDOUT to the file out.log
.
2>& 1
将fd2(STDERR)重定向到fd1(STDOUT),该文件已经重定向到文件 out.log
.
2>&1
redirects fd2 (STDERR) to fd1 (STDOUT), which is already redirected to the file out.log
.
您的问题出在文件权限(或只读文件系统)上.在命令前面加上 sudo
不能解决此问题,因为 sudo
仅以root身份执行 nohup dotnet application.dll
,因此输出重定向为由bash使用您的普通用户权限来完成.您可以通过使用root特权调用单独的shell来解决此问题:
Your problem lies in the file permissions (or a read-only filesystem). Prepending sudo
to your command can't fix this, because only nohup dotnet application.dll
is executed as root by sudo
, the output redirection is done by bash with your normal user privileges. You can work around this by calling a separate shell with root privileges:
sudo sh -c 'nohup dotnet application.dll > out.log 2>&1 &'
这篇关于如何将nohup输出重定向到指定文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!