0

I have a dotnet Server Application that i start like this to run it even when the console is closed. nohup dotnet app.dll --urls=http://adress/ &

i want to create a bash script that restarts it infinite by itself when it has some issue and breaks and found some solution here like this:

while true; do ./app && break; done

I tried to encapsulate my code like this.

while true; do nohup dotnet app.dll --urls=http://adress/ & && break; done

which does not work because of the & && (the first & is a configuration for nohup) Then i tried to create just a second bash script and run it like this.

while true; do ./secondbashscript && break; done

While the second bash script consists of the nohup dotnet line.

But i had some trouble to get it running and i don't want to be stuck in an infinite loop which i expect. Instead it should just start the dotnet application and restart it but don't start it more than once at a time.

  • just run `while true; do dotnet app.dll --urls=http://adress/; done` in a [tag:gnu-screen] or [tag:tmux] window. – pynexj Apr 25 '23 at 08:25
  • if i do that, can i still close the application, because normally the Loop would prevent this without && break. – CamouflagedPenguin Apr 25 '23 at 08:32
  • do u mean the terminal app by "application"? yes the terminal can be closed. gnu-screen/tmux will keep running in background. – pynexj Apr 25 '23 at 09:06

2 Answers2

0

Seems the tool Supervisor will help you with this. The tool can refer the docs.

And I also find a blog teach how to create a daemon process for a dotnet app.

Gary Ox64
  • 71
  • 2
  • Hey Gary. I found out that a daemon process is a better fit. But the Process like this still throws some errors when running. Started manually it works fine but as deamon it breaks after some time. – CamouflagedPenguin Apr 27 '23 at 08:04
  • Hi @CamouflagedPenguin, Did you print the logs and what is the error reported? Where is it not as expected? I see that your configuration `RestartSec=86400` is too long, more details will help me understand the "breaks" better. And I found a [Tutorial](https://learn.microsoft.com/en-us/troubleshoot/developer/webapps/aspnetcore/practice-troubleshoot-linux/2-3-configure-aspnet-core-application-start-automatically) wish will help. – Gary Ox64 Apr 27 '23 at 08:43
  • Hey Gary, i just did a typo and now it works most of the time. There are still some issues but those are not related to the demon process but to the app i am running. Thx for the help. – CamouflagedPenguin May 10 '23 at 07:42
0

i finally found a solution better fit for my problem than using Screen or tools like this and if i understand it right is should run without console running. Otherwise i still need to detach it.

i create a service using systemd in the /etc/systemd/system/ directory with this content:

[Unit]
Description=Application

[Service]
WorkingDirectory=/path/to/your/application
ExecStart=/usr/bin/dotnet /path/to/your/application/Application.dll --urls=http://:*/
Restart=always
RestartSec=86400

[Install]
WantedBy=multi-user.target