قالب وردپرس درنا توس
Home / Tips and Tricks / Restart or shut down Linux by using the command line

Restart or shut down Linux by using the command line



  Terminal on a Linux desktop

Fancy a reboot? In this tutorial, you will learn how to cleanly and safely restart or shut down your Linux or MacOS computer from the command line.

We're going down

Sometimes you just need to reboot or shut down completely. If you are working on a server without a GUI or in an SSH session with a remote computer, the command line is the only option. Linux and Unix-like systems, such as macOS, provide several commands to shut down or restart the system directly from the command line.

The following commands can be used:

  • shutdown
  • reboot
  • halt
  • poweroff

Browsing the man pages for these commands can be confusing. Depending on which command-line options you choose, all of these commands can execute shutdowns, restarts, and system shutdowns. In fact, the man pages for reboot halt and poweroff contain exactly the same information.

 man page for the halt command

What is behind it?

The answer lies in the systemd bootstrap system, which replaced the time-honored system System V init . In the Linux world, Fedora began in 2011 with the use of systemd . Since then, Fedora has been adopted by numerous distributions. Debian and Ubuntu switched to systemd in 2015.

At systemd -based distributions, the standstill reboot was halted The commands and poweroff are effectively abbreviations based on the command indicate systemctl . Maintaining these commands provides some degree of compatibility with distributions based on System V init . This means that shell scripts (and hardcore system V system administrators) do not tip over when they are moved to a computer that is running a systemd distribution.

Shut Down

Shut Down When you restart a system with multiple users, you must plan in advance. You must decide when to shut down or restart, and notify the other system users when the shutdown occurs and when. If it is your own computer and you are the only one using it, life is much easier.

To execute one of these commands, you must be in the group sudo . You must have superuser privileges and be able to use the sudo command. If the command you issue takes effect immediately and does not affect other logged in users, you do not need to use sudo . If you try to use one of these commands and the command is rejected, try again with sudo .

By default, the shutdown command ensures that all processes are stopped cleanly file systems are synchronized and all CPU activity is stopped. This is the "stop" state. It then sends a message to the hardware to turn off the power. This is of course the state "Shutdown" or "Shutdown".

It is common practice to pass shutdown some parameters, such as: For example, a time string and a message to which the logged in users are sent warn them to shut down. Let's plan a shutdown for 15 minutes. Enter shutdown a space, +15 a space and then the message to the user.

  shutdown +15 shutdown in 15 minutes! 

  Shut Down Command +15 Message

The time string we used was +15 . This corresponds to 15 minutes. + is optional. We could have typed 15 .

We receive an answer confirming that a shutdown is planned and when it will take place. Registered users receive the message we provide.

 Shutdown Notification

To cancel a shutdown, use -c (Cancel).

  shutdown - c 

  shutdown -c cancel command

Although you do not receive notification that your shutdown has been canceled, your logged in users will be notified.

 Cancellation Notification [19659005] If you do not specify a time string, a shutdown is scheduled from now on one minute . Note that if you do not specify a time string, you will not be able to provide a message to your logged in users.

  shutdown 

  shutdown command without parameters

If you can not wait even a minute, you can use now as a time string and the deadlock works immediately , The use of now is like the use of +0 .

 Shut Down

. The time string may be a set time, for example, 23:00. It must follow the format HH: MM and must be in 24-hour format. Five minutes before the system failure prevents new applications.

 23:00 shut down with message

We know that the default action of shutdown causes the computer to shut down and then shut down. We can override this behavior by passing other command-line options.

  • The option -H (halt) shuts down your computer but does not shut down the hardware. 19659007] The -P (Poweroff) is the default action. The computer will be stopped and then turned off.
  • The -r (Restart) option shuts down your computer and then restarts it.
  • The -h (stop and turn off) is the same as -P . If you use -h and -H together, the option -H has priority.
  • The -c (abort) This option aborts any scheduled shutdown, pause, or restart.

Here is an example in which we have planned a reboot.

  shutdown -r 08:20 System Restart at 08:20 

  Shutdown Scheduling a Restart scrn4

Restart, Stop, and Shutdown Commands

These commands execute the action who suggests her name. However, each of them accepts command line options to reboot, stop, or power off. But why are things confusing? These commands are best used at face value.

If you want to reboot now, use reboot . If you want to turn off now, use poweroff and if you want to stop the system now, use halt .

  restart 

  restart command in a terminal window

  halt 

  halt command in a terminal window

  poweroff 

  poweroff command in a terminal window

These commands are effective immediately. If one of these commands is rejected, go ahead sudo . Note, however, that a disapproval is usually due to the fact that other users are logged in to the system you are currently taking offline.

Which command is right for me?

In multi-user environments with shutdown Performing these actions gives you more control. The ability to schedule shutdowns and reboots and warn users with a broadcast message is invaluable in these cases. For a single-user computer, reboot and poweroff will likely meet your requirements.




Source link