Linuxシャットダウンコマンドについて
Linuxシャットダウンコマンドは、オペレーティングシステムを安全に停止するために使用されます。ログインしたユーザーは、システムがシャットダウンされるというメッセージを受け取ります。このコマンドを使用すると、システムをすぐに、または設定された期間に従ってシャットダウンできます。このチュートリアルでは、shutdownコマンドを使用する基本的な方法と、それを使用する際のベストプラクティスを学習します。一部の新しいディストリビューションでは、uptimeコマンドがシステムコマンドsystemctlに関連付けられています。さらに、再起動する方法、再起動をスケジュールする方法、ログインしているユーザーに警告する方法などについても説明します。
このトピックでは何を取り上げますか?
- すべてのコマンドの基本構文
- シャットダウン、再起動、ユーザー通知に使用する基本的な方法
- 代替コマンド
- 適応を容易にするための60秒のビデオチュートリアル
Linuxシャットダウンコマンドの基本構文
先に進む前に、コマンドの基本的な構文を見てみましょう。
shutdown [options] [time] [message]
Code language: Bash (bash)
これはマニュアルページの出力です:
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)
シャットダウン–ヘルプ出力
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)
使用の基本的な方法
最初の例では、他のユーザーに警告することなく、オペレーティングシステムをすぐにシャットダウンします。
shutdown -h now
Code language: Bash (bash)
この場合、ssh接続を使用していると、すぐに終了します。次の例では、シャットダウンが2分間延期されます。
# 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)
これで、出力に表示されているように、別のオプションを使用してリクエストをキャンセルできます。
# cancel shutdown request
$ shutdown -c
Code language: Bash (bash)
shutdown -cを実行した後 別の出力は表示されませんが、上記のコマンド shutdown -h 2 終了します。 shutdown -hの代替方法 シャットダウン+ 次の例のように:
# 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)
次に、より実用的な例を作成しましょう。 30分後にサーバーをシャットダウンし、すべてのユーザーにアクションについて通知する必要があります。
# 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)
それよりも優れた方法を1つでも実行できます。たとえば、サーバーが特定の時間にシャットダウンされることをすべてのユーザーに通知したいとします。
# 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)
今すぐシャットダウンを除くすべての例で注意してください 、 shutdown -cを使用できます シャットダウンプロセスをキャンセルします。スケジュールされたサーバーシャットダウンを使用する場合。ほとんどの場合、再起動になります。最新の例は、特定の時間にサーバーのスケジュールされた再起動を実行し、ユーザーに通知するコマンドです。# 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)
警告やスケジュールのない高速再起動方法は、コマンドrebootで実行できます:
# A immediate reboot method
$ reboot
Code language: Bash (bash)
コンピュータの物理的なリセットボタンのように、再起動を強制することができます:
# A immediate forced reboot
$ reboot -f
Code language: PHP (php)
Linuxシャットダウンコマンドの代替コマンド
Haltコマンドの構文と例
Linuxディストリビューションをシャットダウンするためのもう1つの優れたコマンドは、haltコマンドです。 。停止の基本的な構文は次のとおりです。
halt [OPTION]
Code language: Bash (bash)
そして停止の助け:
$ 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)
そして、最後の代替コマンドとして、poweroffを使用できます。構文は次のとおりです。
poweroff [OPTION]
Code language: Bash (bash)
Poweroffコマンドの構文と例
そして電源オフのヘルプ:
$ 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)
これは、上記のコマンドを簡単に適応させるための60秒のビデオチュートリアルです
SYSTEMCTLはどうですか?
この記事の冒頭で、 systemctlについて説明しました。 それについて少しお話しする時が来たと思います。ほとんどの新しいディストリビューションでは、コマンドを使用してオペレーティングシステムをシャットダウンおよび再起動できます。これがボーナスボーナスの使い方のヒントです🙂
# Use System Control to shutdown the operating system
$ systemctl poweroff
-----------------------------------------------------
# Use System Control to restart the operating system
$ systemctl reboot
Code language: Bash (bash)