Explanation
Many friends were already playing computer games in the DOS era! At that time, when we shut down the DOS system, we often turned off the power switch directly. However, when you are unhappy, Windows can be shut down by pressing the power switch for four seconds! But in Linux, this is not recommended!
Why? In a Windows (non-NT host system) system, since it is a single-person fake multitasking situation, even if your computer is shut down, there should be no impact on others! However, under Linux, since each program (or service) is running in the background, there may actually be quite a few people working on your host at the same time behind the screen that you cannot see. For example, browsing the web, sending letters, transferring files via FTP, etc., if you directly press the power switch to shut down, other people's data may be interrupted! That can be nerve-wracking!
In addition, the biggest problem is that if it is shut down abnormally, it may cause damage to the file system (because there is no time to write data back to the file, some service files will have problems!). So under normal circumstances, you need to pay attention to the following things when shutting down:
Observe the system usage status
If you want to see who is online at the moment, you can issue the "who" command.
If you want to check the network connection status, you can issue the command "netstat -a".
If you want to see the programs running in the background, you can run the command "ps -aux".
Using these commands can let you know a little bit about the current usage status of the host! Of course, this will allow you to determine whether the shutdown is possible (these commands will be mentioned in the common Linux commands later!)
Notify online users of the shutdown time: Always notify online users before shutting down It takes some time to finish their work, so at this time you can use the shutdown special command to achieve this function.
The correct shutdown command is
for example shutdown and reboot! So let’s talk about a few commands related to shutdown/restart!
Command to write data to the hard disk synchronously: sync Common shutdown command: shutdown Restart, shutdown: reboot, halt, poweroff
sync
Data running in the computer mode, all data must be read into the memory before it can be processed by the CPU, but the data often needs to be written back to the hard disk from the memory (such as a storage operation). Because the speed of the hard disk is too slow (compared to the memory), if data is often written/read back and forth between the memory and the hard disk, the system performance will not be very good.
So in the Linux system, in order to speed up the reading of data, by default, some data that has been loaded into the memory will not be written directly back to the hard disk, but will be cached in the memory first. Among them, in this way, if a piece of data is rewritten by you repeatedly, since it has not been written to the hard disk, it can be read directly from the memory, and the speed must be much faster!
However, this also causes some trouble, that is, if your system shuts down abnormally due to some special circumstances (such as a power outage or accidentally kicking the power), because the data has not been Write to the hard disk, wow! Therefore, the data upgrade will be abnormal! So what to do? At this time, you need the sync command to write data! Enter sync directly in the text interface, and the data in the memory that has not been updated will be written to the hard disk! Therefore, this command is very important before shutting down or restarting the system! Better to run it a few times!
Although the current shutdown/reboot/halt and other commands have already called the sync tool before shutting down, it is always safer to do it a few times.
shutdown
Since Linux shutdown is such an important task, except when you log in to the system through the tty7 graphical interface in front of the host, you can shut down the system no matter what identity you use. If you use Remote management tools (such as using ssh service through pietty to log in to the host from other computers), then only root has the power to shut down!
Um! Then let’s turn off the phone and give it a try! What we commonly use is the shutdown command, which notifies various programs (processes) in the system and notifies some services in the run-level in the system to shut down. Shutdown can achieve the following tasks:
You can freely choose the shutdown mode: shut down, restart or enter single-player operation mode; the shutdown time can be configured: it can be configured to shut down immediately now, or you can configure a certain Shut down after a specific time. Shutdown information can be customized: Before shutting down, you can send your own configured information to the online user. You can just issue a warning message: sometimes you may want to conduct some tests and don't want other users to interfere, or you can clearly tell users to pay attention to it for a certain period of time! At this time, you can use shutdown to scare the user, but it does not really mean shutting down! You can choose whether you want fsck to check the file system.
[root@www ~]# /sbin/shutdown [-t seconds] [-arkhncfF] Time [warning message]
Options and parameters:
-t sec: -t followed by seconds Number, which means "shut down after a few seconds"
-k: Don't actually shut down, just send a warning message!
-r : Restart the system after stopping the service (commonly used)
-h : Shut down the system immediately after stopping the system service. (Commonly used)
-n : Directly use the shutdown function to shut down the system without going through the init program
-f : After shutting down and starting, forcefully skip fsck's disk check
-F : After the system restarts, Force fsck disk check
-c: Cancel the contents of the shutdown command that is already in progress.
Time: This is a parameter that must be added! Specify the time for system shutdown! Examples of time are explained below.
Example:
[root@www ~]# /sbin/shutdown -h 10 'I will shutdown after 10 mins'
# Let me tell you, this machine will shut down after ten minutes! And it will be displayed in front of the current login screen!
# What are the parameters? Let’s introduce a few below!
In addition, it should be noted that the time parameter must be added to the command, otherwise the shutdown will automatically jump to run-level 1 (which is the login situation for single-person maintenance), which will cause damage. Got a brain! Here are some examples of time parameters:
[root@www ~]# shutdown -h now
Shut down immediately, where now is equivalent to the state of time 0
[root@www ~ ]# shutdown -h 20:25
The system will be shut down at 20:25 today. If this command is issued at 21:25, it will be shut down the next day
[root@www ~]# shutdown -h +10
The system will automatically shut down after another ten minutes
[root@www ~]# shutdown -r now
The system will restart immediately
[root@www ~]# shutdown -r +30 'The system will reboot'
The system will reboot in thirty minutes and display the following information to all online users
[root@www ~]# shutdown -k now 'This system will reboot'
Only parameters for issuing warning letters! The system will not shut down! Scare people!
reboot, halt, poweroff
There are three commands that can perform restart and shutdown tasks, namely reboot, halt, poweroff. In fact, the function libraries called by these three commands are similar, so when you use "man reboot", the usage of the three commands will appear for you at the same time.
Since these commands can shut down or restart the computer, is there any difference? Basically, by default, these commands will complete the same job! (Because halt will call shutdown first, and shutdown will finally call halt!). However, shutdown can close each service one by one according to the currently started services before shutting down; as for halt, it can perform the special function of hardware shutdown without paying attention to the current system status! You can use root on your host to execute the following two commands to shut down the computer and compare them to see the difference!
The above is the content of note 002 on the correct shutdown method of Linux. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!