Home  >  Article  >  System Tutorial  >  The difference between linux restart commands

The difference between linux restart commands

WBOY
WBOYOriginal
2024-02-22 09:09:03433browse

The difference between Linux restart commands requires specific code examples

  1. About restart commands
    In Linux systems, there are many restart commands that can be used, each command has its own unique purpose and features. In this article, we will introduce three commonly used restart commands: reboot, shutdown and init.
  2. 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.

  3. 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:

  4. -r: means restarting the system;
  5. -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.

  1. 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.

  2. 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:

  • reboot command is the most direct and commonly used restart command;
  • shutdown command provides more options and flexibility and can be used Scheduled shutdown and cancellation of scheduled shutdown operations; the
  • init command can be used to initialize the system and control the running level of the system.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn