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:
- A Linux server where your ASP.NET Core app is deployed.
- 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:
1 |
apt install incron |
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:
1 2 |
incrontab -e |
Add the following line to the table:
1 2 |
/var/www/myapp IN_CLOSE_WRITE systemctl restart myapp.service |
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:
1 2 |
systemctl restart incron |
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:
1 2 3 |
/var/www/myapp/myapp.dll IN_CLOSE_WRITE sleep 30 && sudo systemctl restart myapp.service /var/www/myapp/appsettings.json IN_CLOSE_WRITE sleep 30 && sudo systemctl restart myapp.service |
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!