How can I redirect nohup output to a specified file?

倾然丶 夕夏残阳落幕 提交于 2021-02-04 15:28:31

问题


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 &

I always receive something like

-bash: out.log: Permission denied

I have tried running the application using sudo but it still doesn't seem to work.


nohup dotnet application.dll &

Works fine, but it directs the output to some other directory like /home/ubuntu/nohup.out

What am I doing wrong?


回答1:


nohup dotnet application.dll > out.log 2>&1 & is the correct form.

> out.log redirects STDOUT to the file out.log.

2>&1 redirects fd2 (STDERR) to fd1 (STDOUT), which is already redirected to the file out.log.

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 &'


来源:https://stackoverflow.com/questions/49467245/how-can-i-redirect-nohup-output-to-a-specified-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!