How to fix long server shutdown times in Linux

Giteqa

Your Linux system takes too long to shut down? Here are the steps you can take to figure out what's causing the delayed shutdown and fix the problem.

I hope you are a little familiar with the concept of sigterm and sigkill.

When you shutdown your Linux system, it sends sigterm and politely asks you to stop running processes. Some processes don't work correctly, ignore sigterm and keep running.

This can delay the shutdown process because your system will wait for a predetermined period of time to stop running processes. After this period of time, it sends a kill signal to forcefully stop all remaining running processes and shuts down the system. I recommend reading about sigterm vs sigkill to understand the difference.

In fact, in some cases you may see a message like «the job is stopped» on a black screen.

If your system takes too long to shut down, you can do the following:

Check which process / service is taking too long and if you can uninstall or reconfigure it to work correctly.
Change the default timeout period before your system forcibly stops running processes. [Quick and dirty fix]
Here I am using Ubuntu which uses systemd. The commands and steps given here are valid for any Linux distribution that uses systemd (most of them do).

Troubleshoot Linux long shutdowns

Check which processes are causing long shutdowns on Linux
If you want to find out what happened, you should check what happened on the last shutdown. Use this command to get more information::

journalctl -rb -1
The journalctl command allows you to read the system logs. Using parameters «-b -1» you are filtering the logs for the last download session. When choosing an option «-r» the logs are displayed in reverse chronological order.

The command journalctl -rb -1 will show the system logs just before your Linux system was last shut down. This is what you need to analyze the Linux long outage problem.

No logs? Here's what you should do

Did you find anything suspicious in the logs? Is there a process / service that refuses to stop? If so, find out if it can be removed without side effects or reconfigured. Please do not blindly delete things. You should know this process.

Speed ​​up shutdowns on Linux by shortening the default shutdown timeout [Quick fix]
The default shutdown timeout is typically 90 seconds. Your system is trying to force stop services after this period of time.

If you want your Linux system to shutdown quickly, you can change this timeout period.

You will find all systemd settings in the configuration file located at /etc/systemd/system.conf. This file should be filled with many lines starting with #. They are the default values ​​for the entries in the file.

Before doing anything else, it's a good idea to make a copy of the original file.

sudo nano /etc/systemd/system.conf /etc/systemd/system.conf.orig
Find here DefaultTimeoutStopSec. It should probably be set to 90 seconds.

# DefaultTimeoutStopSec = 90 с
You should change this value to something more convenient, for example 5 or 10 seconds.

DefaultTimeoutStopSec = 5 с.
Change Ubuntu Shutdown Time Settings

Remember to remove the # beforeDefaultTimeoutStopSec. Save the file and reboot your system.

This should help you reduce the shutdown delay on your Linux system.

How long does it take for your Linux system to boot?

This can be found using the command systemd-analysis.

Did you fix the long disconnect?

I hope this guide helped you investigate and fix the long shutdown issue on your system. Let me know in the comments if you managed to fix this.