【发布时间】:2018-09-03 21:35:46
【问题描述】:
我遇到了一些奇怪的问题,我不确定如何解决。我正在使用 CodeDeploy 将我的应用程序部署到 AWS EC2 实例,我需要在其中指定一个脚本来在我的实例上执行进程,其中一个是 .NET Core 应用程序。
我在 Windows 上使用 dotnet publish 发布我的应用程序,并将所需的 dll 复制到 EC2 Linux 实例。
我可以很好地运行应用程序,并且它的行为符合预期
sudo dotnet application.dll
但这只是在我终止应用程序并且不在后台之前。
当我尝试在后台运行我的应用程序时出现问题,以便我可以继续运行其他应用程序或执行其他任务。
我通常使用screen 或nohup 在后台运行应用程序,但它似乎不适用于此应用程序。 screen 在脚本中不起作用,nohup 运行应用程序,但抛出错误
要使用 nohup 运行,我使用的是 sudo nohup dotnet application.dll &,我在日志中收到错误
Unhandled Exception: System.UnauthorizedAccessException: Access to the path is denied. ---> System.IO.IOException: Bad file descriptor
--- End of inner exception stack trace ---
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
at Interop.CheckIo(Int64 result, String path, Boolean isDirectory, Func`2 errorRewriter)
at System.ConsolePal.Read(SafeFileHandle fd, Byte[] buffer, Int32 offset, Int32 count)
at System.ConsolePal.UnixConsoleStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadLine()
at System.IO.SyncTextReader.ReadLine()
at System.Console.ReadLine()
at Application.Program.Main(String[] args) in F:\Applications\Server\Program.cs:line 38
现在我可以看到为什么会出现这种情况,因为错误中的路径是我在 Windows 上的项目的路径,但为什么只使用 nohup 会发生这种情况?如果我在前台运行它并且不抛出错误,这工作正常。
如何在不使用屏幕的情况下在后台运行此应用程序?
编辑
我最初的想法是因为它是在 Windows 上构建的,所以这是一些兼容性问题,但事实并非如此。我已将项目移至 Linux 实例并在那里重新发布,但在运行sudo nohup dotnet application.dll &时仍然收到相同的错误@
Unhandled Exception: System.UnauthorizedAccessException: Access to the path is denied. ---> System.IO.IOException: Bad file descriptor
--- End of inner exception stack trace ---
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
at Interop.CheckIo(Int64 result, String path, Boolean isDirectory, Func`2 errorRewriter)
at System.ConsolePal.Read(SafeFileHandle fd, Byte[] buffer, Int32 offset, Int32 count)
at System.ConsolePal.UnixConsoleStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadLine()
at System.IO.SyncTextReader.ReadLine()
at System.Console.ReadLine()
at Application.Program.Main(String[] args) in /opt/servers/Server/Program.cs:line 38
我刚试过
sudo dotnet application.dll > out.log 2>&1 &
这似乎可以正常运行程序,但是每当执行另一个命令时,后台应用程序就会进入STOPPED 状态并且不会继续运行。
[1]+ Stopped sudo dotnet application.dll > out.log 2>&1
ubuntu@:/opt/servers/Server$ sudo dotnet application.dll > out.log 2>&1 &
[1] 2448
ubuntu@:/opt/servers/Server$ netstat -tulpn
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
udp 214656 0 0.0.0.0:1000 0.0.0.0:* -
udp 0 0 0.0.0.0:68 0.0.0.0:* -
udp 0 0 0.0.0.0:68 0.0.0.0:* -
[1]+ Stopped sudo dotnet application.dll > out.log 2>&1
下一步将研究在分离模式下使用screen。
screen -d -m bash -c 'cd /opt/server && sudo dotnet application.dll &'
此命令当前不生成屏幕窗口...
编辑
我不得不在最后删除&,现在它可以按预期工作了。
screen -d -m -S SERVER bash -c 'cd /opt/server && sudo dotnet application.dll'
【问题讨论】: