title: How can I run a dotnet application in the background using sudo on Linux?
body: <p>I am experiencing some weird problems that I'm not sure how to solve. I am using CodeDeploy to deploy my applications to an AWS EC2 instance, and in there I need to specify a script to execute the processes on my instances, one of which is a .NET Core application.</p>
  
  <p>I am publishing my application using <code>dotnet publish</code> on Windows, and copying the required dlls to the EC2 Linux instance.</p>
  
  <p>I can run the application fine using and this behaves as expected</p>
  
  <pre><code>sudo dotnet application.dll
  </code></pre>
  
  <p>However this is simply until I terminate the application and is not in the background.</p>
  
  <p>The problem is happening when I am trying to run my application in the background, so that I can continue running other applications or perform other tasks.</p>
  
  <p>I usually use <code>screen</code> or <code>nohup</code> to run applications in the background however it doesn't seem to work for this application. Screen doesn't work in the script, and nohup runs the application, but throws an error</p>
  
  <p>To run using nohup I am using <code>sudo nohup dotnet application.dll &amp;</code> and I receive the error in the log</p>
  
  <pre><code>Unhandled Exception: System.UnauthorizedAccessException: Access to the path is denied. ---&gt; 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
  </code></pre>
  
  <p>Now I can see why this is thrown, as the path in the error is the path to my project on Windows, but why is this happening only using nohup? This works fine if I run it in the foreground and does not throw the error.</p>
  
  <p>How can I run this application in the background without using screen?</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>My initial thought was that is was some compatibility issue as it was built on Windows, however this is not the case. I have moved the project over to the Linux instance and re-published it there, and I am still receiving the same error when running <code>sudo nohup dotnet application.dll &amp;</code></p>
  
  <pre><code> Unhandled Exception: System.UnauthorizedAccessException: Access to the path is denied. ---&gt; 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
  </code></pre>
  
  <hr>
  
  <p>I have just tried</p>
  
  <p><code>sudo dotnet application.dll &gt; out.log 2&gt;&amp;1 &amp;</code></p>
  
  <p>which seems to run the program without error, however whenever another command is performed, the background application is sent into a <code>STOPPED</code> state and does not continue to run.</p>
  
  <p><code>[1]+  Stopped                 sudo dotnet application.dll &gt; out.log 2&gt;&amp;1</code></p>
  
  <pre><code>ubuntu@:/opt/servers/Server$ sudo dotnet application.dll &gt; out.log  2&gt;&amp;1 &amp;
  [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 &gt; out.log 2&gt;&amp;1
  </code></pre>
  
  <hr>
  
  <p>Next step would be to look into using <code>screen</code> in detached mode.</p>
  
  <p><code>screen -d -m bash -c 'cd /opt/server &amp;&amp; sudo dotnet application.dll &amp;'</code></p>
  
  <p>This command currently doesn't spawn a screen window...</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I had to remove the <code>&amp;</code> at the end and now it works as expected.</p>
  
  <p><code>screen -d -m -S SERVER bash -c 'cd /opt/server &amp;&amp; sudo dotnet application.dll'</code></p>