Home >System Tutorial >LINUX >The difference between linux restart commands
The difference between Linux restart commands requires specific code examples
reboot command
Thereboot command is the most direct and commonly used restart command. Use the reboot command to restart the system immediately without further confirmation. Its basic syntax is as follows:
reboot
After executing this command, the system will shut down and restart immediately. This command usually requires administrator privileges to execute.
shutdown command
The shutdown command can also be used to restart the system, but it provides more options and flexibility. The shutdown command can not only restart the system, but can also be used to schedule shutdown, cancel planned shutdown operations, etc. The basic syntax is as follows:
shutdown [选项] 时间 [警告信息]
Among them, the options include:
-r
: means restarting the system; -h
: Indicates shutdown. The time parameter can be a specific time or a period of time later. For example, to restart the system after 10 minutes, you can use the following command:
shutdown -r +10 "系统将在10分钟后重启"
The warning message parameter is optional and will display a message to all users before the system shuts down.
init command
The init command is a very important command in the Linux system. It can be used to initialize the system and control the running level of the system.
To use the init command to restart the system, you need to specify the run level as 6. Runlevel 6 corresponds to rebooting the system. Its basic syntax is as follows:
init 6
This command will immediately shut down and restart the system. Similar to the reboot command, this command usually requires administrator privileges to execute.
Sample Code
To better understand the difference between these restart commands, let's demonstrate a sample code. Suppose we have a script file with the path /home/user/restart_script.sh
and the content is as follows:
#!/bin/bash echo "开始执行重启操作..." reboot echo "脚本执行完毕!"
This script file uses the reboot command to restart the system. Now, we will demonstrate how to use the other two commands to achieve the same functionality.
First, use the shutdown command to restart the system. Execute the following command in the terminal:
sudo shutdown -r +1 "系统将在1分钟后重启,请保存您的工作。"
This command will restart the system after 1 minute and display a warning message to all users before restarting.
Secondly, use the init command to restart the system. Execute the following command in the terminal:
sudo init 6
This command will immediately shut down and restart the system.
No matter which restart command is used, administrator privileges are required to execute it. This is because the restart operation poses certain risks to the system and requires the administrator's authorization to perform it.
Summary:
I hope this article will help you understand the differences between Linux restart commands. I wish you a happy use of the Linux operating system!
The above is the detailed content of The difference between linux restart commands. For more information, please follow other related articles on the PHP Chinese website!