GNU/Linux >> Linux Esercitazione >  >> Linux

Come utilizzare il comando di spegnimento di Linux, riavvii pianificati e immediati

Informazioni sul comando di arresto di Linux

Il comando di arresto di Linux viene utilizzato per arrestare il sistema operativo in modo sicuro. Gli utenti che hanno effettuato l'accesso ricevono un messaggio di chiusura del sistema. Il comando consente al sistema di spegnersi immediatamente o in base a un determinato periodo di tempo. In questo tutorial imparerai i modi di base per utilizzare il comando di spegnimento e le migliori pratiche per usarlo. In alcune delle distribuzioni più recenti, il comando uptime è associato al comando di sistema systemctl. Inoltre, esploreremo come riavviare, come pianificare i riavvii, come avvisare gli utenti che hanno effettuato l'accesso e altro ancora.

Cosa tratteremo in questo argomento?

  • Sintassi di base per tutti i comandi
  • Pratiche di base da utilizzare per l'arresto, il riavvio e l'avviso per l'utente
  • Comandi alternativi
  • Tutorial video di 60 secondi per un adattamento più semplice

Sintassi di base del comando di arresto di Linux

Prima di andare avanti, diamo un'occhiata alla sintassi di base del comando.

shutdown [options] [time] [message]
Code language: Bash (bash)

Questo è l'output della pagina man:

shutdown may be used to halt, power-off or reboot the machine.

The first argument may be a time string (which is usually "now").
Optionally, this may be followed by a wall message to be sent to all
logged-in users before going down.

The time string may either be in the format "hh:mm" for hour/minutes
specifying the time to execute the shutdown at, specified in 24h clock
format. Alternatively it may be in the syntax "+m" referring to the
specified number of minutes m from now.  "now" is an alias for "+0",
i.e. for triggering an immediate shutdown. If no time argument is
specified, "+1" is implied.

Note that to specify a wall message you must specify a time argument,
too.

If the time argument is used, 5 minutes before the system goes down the
/run/nologin file is created to ensure that further logins shall not be
allowed.
Code language: Bash (bash)

spegnimento – output della guida

shutdown --help
shutdown [OPTIONS...] [TIME] [WALL...]

Shut down the system.

Options:
     --help      Show this help
  -H --halt      Halt the machine
  -P --poweroff  Power-off the machine
  -r --reboot    Reboot the machine
  -h             Equivalent to --poweroff, overridden by --halt
  -k             Don't halt/power-off/reboot, just send warnings
     --no-wall   Don't send wall message before halt/power-off/reboot
  -c             Cancel a pending shutdown

Code language: Bash (bash)

Pratiche di base per l'uso

Il primo esempio arresterà immediatamente il sistema operativo senza alcun avviso per gli altri utenti:

shutdown -h now
Code language: Bash (bash)

In questo caso, se stai utilizzando una connessione ssh, verrà interrotta immediatamente. Nel prossimo esempio lo spegnimento verrà posticipato di due minuti:

# shutdown -h (time in minutes)
$ shutdown -h 2
Shutdown scheduled for Mon 2021-03-22 18:52:31 EET, use 'shutdown -c' to cancel.
Code language: Bash (bash)

Ora come vediamo nell'output possiamo usare un'altra opzione per annullare la richiesta:

# cancel shutdown request
$ shutdown -c
Code language: Bash (bash)

Dopo aver eseguito spegnimento -c non vedremo un altro output ma il comando precedente shutdown -h 2 sarà terminato. Un metodo alternativo allo spegnimento -h è arresto + come il prossimo esempio:

# Shutdown server after 10 minutes
$ shutdown +10
Shutdown scheduled for Mon 2021-03-22 19:04:53 EET, use 'shutdown -c' to cancel.
Code language: Bash (bash)

Ora creiamo un esempio più pratico. Vogliamo spegnere il server dopo 30 minuti e tutti gli utenti devono essere informati sulla nostra azione.

# Shutdown the server after 30 minutes and inform logged users
$ shutdown +30 "The server will be shutdown for maintenance after 30 minutes please log out all of your sessions!"
Shutdown scheduled for Mon 2021-03-22 19:28:27 EET, use 'shutdown -c' to cancel.

Code language: Bash (bash)

Possiamo fare anche di meglio, diciamo che vogliamo informare tutti gli utenti che il server verrà spento a un'ora specifica:

# Shutdown the server at a specific time and message
$ shutdown -h 21:00 "The server will be shutdown for maintenance at 21:00 please log out all of your sessions!"
Shutdown scheduled for Mon 2021-03-22 21:00:00 EET, use 'shutdown -c' to cancel.

Code language: Bash (bash)
Tieni presente che in tutti gli esempi tranne spegnimento ora , puoi utilizzare spegnimento -c per annullare il processo di arresto.Se si utilizzano arresti del server pianificati. Molto probabilmente saranno riavvii. Il nostro ultimo esempio potrebbe essere un comando che eseguirà un riavvio pianificato del server a un'ora specifica e avviserà gli utenti.
# Scheduled server restart
$ shutdown -h 21:00 "The server will be restarted for maintenance at 21:00 on 23-03-2021 please log out all of your sessions!" -r
Reboot scheduled for Tue 2021-03-23 21:00:00 EET, use 'shutdown -c' to cancel.

Code language: Bash (bash)

Un metodo di riavvio rapido senza alcun avviso o pianificazione può essere eseguito con il comando reboot:

# A immediate reboot method
$ reboot
Code language: Bash (bash)

Un riavvio può essere forzato come un pulsante di ripristino fisico su un computer:

# A immediate forced reboot
$ reboot -f Code language: PHP (php)

Comandi alternativi per il comando di arresto di Linux

Sintassi ed esempi del comando di arresto

Un altro ottimo comando per chiudere una distribuzione Linux è il comando hat . Ecco la sintassi di base per halt:

halt [OPTION]
Code language: Bash (bash)

E l'aiuto per l'arresto:

$ halt --help
halt [OPTIONS...]

Halt the system.

Options:
     --help      Show this help
     --halt      Halt the machine
  -p --poweroff  Switch off the machine
     --reboot    Reboot the machine
  -f --force     Force immediate halt/power-off/reboot
  -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
  -d --no-wtmp   Don't write wtmp record
     --no-wall   Don't send wall message before halt/power-off/reboot

See the halt(8) man page for details.

Code language: Basic (basic)

E per il comando alternativo finale puoi usare poweroff, ecco la sintassi:

poweroff [OPTION]
Code language: Bash (bash)

Sintassi ed esempi del comando di spegnimento

E l'aiuto per lo spegnimento:

$ poweroff --help
poweroff [OPTIONS...]

Power off the system.

Options:
     --help      Show this help
     --halt      Halt the machine
  -p --poweroff  Switch off the machine
     --reboot    Reboot the machine
  -f --force     Force immediate halt/power-off/reboot
  -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
  -d --no-wtmp   Don't write wtmp record
     --no-wall   Don't send wall message before halt/power-off/reboot

See the halt(8) man page for details.

Code language: Bash (bash)

Ecco un video tutorial di 60 secondi per un più facile adattamento ai comandi precedenti

E SYSTEMCTL?

All'inizio di questo articolo abbiamo menzionato systemctl e pensiamo che sia ora di dire qualche parola a riguardo. Nella maggior parte delle nuove distribuzioni è possibile utilizzare il comando per arrestare e riavviare il sistema operativo. Ecco un suggerimento bonus bonus su come usarlo 🙂

# Use System Control to shutdown the operating system
$ systemctl poweroff
-----------------------------------------------------
# Use System Control to restart the operating system
$ systemctl reboot
Code language: Bash (bash)

Ci auguriamo che questo articolo ti sia piaciuto, se è così, valuta questa pagina con le stelle qui sotto e iscriviti al nostro canale YouTube.


Linux
  1. Che cos'è un comando Chown in Linux e come usarlo

  2. Come utilizzare il comando di arresto di Linux in Ubuntu 18.04 e CentOS 7

  3. Come usare il comando Su in Linux

  4. Come creare un alias e utilizzare il comando alias in Linux

  5. Come installare e utilizzare il comando Ping in Linux

Come utilizzare il comando Declare in Linux Bash Shell

Come utilizzare il comando echo negli script Bash in Linux

Come installare e utilizzare il comando Ping in Linux

Come installare e utilizzare il comando fd in Linux

Come utilizzare il comando di arresto di Linux

Linux perf:come utilizzare il comando e il profiler