Have you ever encountered problems such as terminal lag and slow shutdown in Linux system? These problems not only affect the user experience, but may also affect work efficiency and system stability. This article will introduce some methods of Linux system optimization to help you improve system performance and avoid lagging and long shutdown problems.
I hope you are somewhat familiar with the concepts of sigterm and sigkill.
When you shut down a Linux system, it sends a termination signal (sigterm) and politely asks the running process to stop. Some processes do not conform to this behavior and ignore the termination signal and continue running.
This may cause a delay in the shutdown process as your system waits for a predefined period of time for running processes to stop. After this period, it sends a kill signal to forcefully stop all remaining running processes and shut down the system.
In fact, in some cases, you will see a message similar to "a stop job is running" on the black screen.
If your system is down for too long, you can do the following:
Check which process/service is taking too long and whether you can remove or reconfigure it so that it runs properly.
Change the default waiting time before the system forcibly stops a running process. (Quick and not elegant way)
My operating system is Ubuntu using systemd. The commands and steps here apply to any Linux distribution using systemd (which is most distributions).
Check which processes will cause a long shutdown of Linux
If you want to find out what the problem is, you should check what happened the last time you shut down. Use this command to get the power of "I Know What You Did Last Summer" (a pun on "I Know What You Did Last Summer").
journalctl -rb -1
journalctl command allows you to read system logs. Use option -b -1 to filter the logs for the last session started. When using option -r, the logs are displayed in reverse chronological order.
In other words, the journalctl -rb -1 command will display the system log before the last shutdown of the Linux system. This is where you need to analyze the long shutdown problem of your Linux system.
Don’t have a journal? Here’s what you should do
If there is no journal log, please confirm whether your distribution uses systemd.
Even on some Linux distributions using systemd, journal logging is not activated by default.
Please confirm whether /var/log/journal exists. If it doesn't exist, create it:
sudo mkdir /var/log/journal
You should also check the contents of the /etc/systemd/journald.conf file and make sure the Storage value is set to auto or persistent.
Do you see anything suspicious in the logs? Is there a process/service that refuses to stop? If so, investigate if it can be removed without side effects, or if it can be reconfigured. Please don't blindly delete things here. You should have some idea of this process.
Speed up shutdown in Linux by reducing default stop timeout (quick fix)
The default wait time for shutdown is usually set to 90 seconds. After this time, your system will attempt to force stop the service.
If you want your Linux system to shut down quickly, you can change this waiting time.
You can find all systemd settings in the configuration file located at /etc/systemd/system.conf. There should be many lines starting with # in this file. They represent the default values for each entry in the file.
Before starting, it's a good idea to make a copy of the original file.
sudo cp /etc/systemd/system.conf /etc/systemd/system.conf.orig
Look for DefaultTimeoutStopSec here. It may be set to 90 seconds.
#DefaultTimeoutStopSec=90s
You have to change this value to something more convenient, such as 5 seconds or 10 seconds.
DefaultTimeoutStopSec=5s
If you don’t know how to edit the configuration file in the terminal, you can use this command to open the file for editing in the system’s default text editor (such as Gedit):
sudo xdg-open /etc/systemd/system.conf
Don’t forget to delete the # before DefaultTimeoutStopSec. Save the file and restart the system.
This will help you reduce the shutdown delay of your Linux system.
Watchdog problem!
Linux has a module called watchdog that monitors whether certain services are running. It can be configured to automatically restart the system if it hangs due to a software error.
It is unusual to use watchdogs on desktop systems because you can manually shut down or restart the system. It is often used on remote servers.
First check if the watchdog is running:
ps -af | grep watch*
If your system is running a watchdog, you can change the ShutdownWatchdogSec value from 10 minutes to a lower value in the systemd configuration file /etc/systemd/system.conf.
Through the introduction of this article, you have already learned about some Linux system optimization methods, including disabling services, adjusting kernel parameters, upgrading drivers, cleaning junk files, etc. Through these methods, you can improve system performance and avoid problems such as terminal freezes and slow shutdowns. Of course, different optimization methods are required for different systems and usage scenarios. In practical applications, you can choose the appropriate optimization strategy according to the specific situation and adjust it according to the actual effect.
The above is the detailed content of The terminal freezes and shuts down slowly? Linux system optimization and speed-up guide is here!. For more information, please follow other related articles on the PHP Chinese website!