Auto-restart ASP.NET Core apps after deployment on Linux using incron How to automatically restart your ASP.NET Core web applications hosted on your Linux server after the deploy using incron

Linux - Come inviare E-Mail con sSMTP (con configurazioni-tipo per GMail, Aruba e Yahoo)

When deploying ASP.NET Core applications on Linux servers, it’s essential to ensure that the application restarts automatically after each deployment, as well as system updates or unexpected crashes: unfortunately, the ASP.NET Core doesn't provide such a feature out-of-the-box.

One of the best ways I found to fill this gap is by using incron, a freely available cron-like system that triggers custom actions based on filesystem events.

In this article, we’ll walk through the steps to set up automatic restarts for your ASP.NET Core app using incron.

Prerequisites

Before proceeding, make sure you have the following prerequisites:

  1. A Linux server where your ASP.NET Core app is deployed.
  2. The ASP.NET Core app is already configured and running (following this official guide or a similar approach based on kestrel).

Set Up Auto-Restart with Incron

First, install incron on your Linux server if it’s not already installed. You can do this using the package manager specific to your distribution (e.g., apt, yum, or dnf).

For example, if you use Ubuntu 18+, you can use following command:

Once done, create an incron job that monitors changes in a specific directory (where your ASP.NET Core app resides).

For example, let’s assume your app is located in /var/www/myapp and runs using a kestrel service called myapp.service.

Edit the incron table using the command:

Add the following line to the table:

This line specifies that whenever a file in the /var/www/myapp directory is closed for writing (i.e., updated), it will trigger the command to restart the myapp.service.

After adding the incron job, reload the incron daemon to apply the changes:

Testing it up

Now that incron is up and running, you just have to see if the auto-restart task we have added works like expected.

To quickly test it, make a change to your ASP.NET Core app (e.g., update a configuration file or modify a code file). Save the changes, and incron should automatically restart your app.

Fine tuning

Auto-restarting the app on any file change might be a bit too much: for example, if your deployment task is not fast, you'll risk to face multiple reloads before completing the deployment, which is definitely an undesirable effect. To mitigate such behavior, you might think to limit the "reload task" to a limited number of relevant files that will always get modified, such as:

  • the app's main DLL file - myapp.dll (in our example)
  • the app's configuration file - appsettings.json

Here's the modified configuration lines to add in the incrontab file, replacing the previous one:

With such settings, your web app will be automatically reloaded if (and only if) the myapp.dll or appsettings.json files are modified - which should happen only once for each deployment task. it's also worth noting that we added a sleep 30 command switch to each configuration line: that switch instructs incron to perform the reload after a 30-second sleep time, which will (hopefully) increase the chance that the deployment will be complete. Needless to say, feel free to change that sleep time as you see fit.

Additional Considerations

  • Permissions: Ensure that the user running incron has sufficient permissions to restart the ASP.NET Core app. You might need to adjust ownership or permissions on relevant files and directories.
  • Logging: Set up proper logging for your ASP.NET Core app to track any issues related to automatic restarts.
  • Monitoring: Monitor the incron logs and your app’s behavior to ensure smooth restarts.

Conclusion

By configuring incron to monitor changes in your ASP.NET Core app’s directory, you can achieve automatic restarts after deployment or crashes. This ensures that your app remains up-to-date and responsive without manual intervention.

Remember to adapt the paths and service names according to your specific setup. Happy coding!

Additional references

  1. Stack Overflow: How to restart ASP.NET Core app programmatically
  2. Configure the ASP.NET Core application to start automatically
  3. Run ASP.NET Core app under Linux on startup

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.