GNU/Linux >> Linux の 問題 >  >> Linux

例を含むNohupコマンド

Nohupは「ハングアップなし」の略で、名前が示すように、ログアウトまたはシェルを終了するときにコマンドが自動的に中止されるのを防ぎます。

Linuxコマンドを実行し、そのコマンドの実行が終了していない場合にセッションからしばらくしてログアウトすると、そのコマンドのプロセスが強制終了されます。

完了するのに何時間もかかるあらゆる種類のプログラムがあります。コマンドがタスク全体を完了するのを待つ必要はありません。 nohupコマンドを使用してプログラムをバックグラウンドで実行し続け、後で出力を確認できます。

nohupコマンドの構文

nohupコマンドの構文は次のとおりです。

nohup COMMAND [ARG]...

デフォルトでは、コマンドの出力は、プロセスを開始した端末である標準出力デバイスに表示されます。ただし、子プロセスの存続期間中に端末が閉じられる可能性があるため、端末を出力デバイスにすることはできなくなります。 nohupコマンドの情報ページによると

If standard output is a terminal, the command's standard output is appended to the file `nohup.out'; if that cannot be written to, it is appended to the file `$HOME/nohup.out'; and if that cannot be written to, the command is not run. Any `nohup.out' or `$HOME/nohup.out' file created by `nohup' is made readable and writable only to the user, regardless of the current umask settings.

同様に、標準エラーの場合、

If standard error is a terminal, it is normally redirected to the same file descriptor as the (possibly-redirected) standard output. However, if standard output is closed, standard error terminal output is instead appended to the file `nohup.out' or `$HOME/nohup.out' as above.

これは、デフォルトで標準出力と標準エラーの両方が「nohup.out」ファイルにリダイレクトされることを意味します。ただし、出力を他のファイルにリダイレクトする場合は、リダイレクト演算子を使用していつでもリダイレクトできます。たとえば、makeの出力をキャッチするには、

nohup make > make.log

標準入力の場合、

If standard input is a terminal, it is redirected from `/dev/null' so that terminal sessions do not mistakenly consider the terminal to be used by the command. This is a GNU extension; programs intended to be portable to non-GNU hosts should use `nohup COMMAND [ARG]...

nohupコマンドは、コマンドを自動的にバックグラウンドに配置しません。これは&で明示的に行う必要があります。

nohup make > make.log &

また、このコマンドは、コマンドの良さ(つまり優先度)を変更しません。このために、たとえば「nohupniceCOMMAND」のようにniceコマンドが使用されます。

nohupコマンドの使用方法

nohupは2つの方法で使用できます。 1つ目は、作業中に他の入力を受け取らずに、パラメーターを使用してコマンドを実行することです。エラーメッセージを含むすべての出力は、作業ディレクトリまたはホームディレクトリのファイルnohup.outに書き込まれます。ログアウトまたは端末を閉じたときにコマンドがまだ実行されている場合、コマンドは終了しません。

memtesterアプリケーションの例を試してみましょう。

$ nohup sudo memtester 2048 5
nohup: ignoring input and appending output to 'nohup.out'
Killed

$ cat nohup.out 
memtester version 4.3.0 (64-bit)
Copyright (C) 2001-2012 Charles Cazabon.
Licensed under the GNU General Public License version 2 (only).

pagesize is 4096
pagesizemask is 0xfffffffffffff000
want 2048MB (2147483648 bytes)
got  1876MB (1968074752 bytes), trying mlock ...

>シンボルを使用して、nohup.outの代わりに出力を他のファイルにリダイレクトすることもできます。

$ nohup sudo memtester 2048 5 > result.txt

「&」でnohupコマンドを使用する

プロセスを開始してバックグラウンドに置くには、基本的にコマンドの後に「&」記号を付けて実行します。コマンドの最後にある「&」記号は、bashにnohup yourcommandを実行するように指示します。 バックグラウンドで。 fgコマンドを使用してフォアグラウンドに戻すことができます。

$ nohup bash sleep1.sh &
[1] 653
$ nohup: ignoring input and appending output to 'nohup.out'
fg
-bash: fg: job has terminated
[1]+  Exit 127                nohup bash sleep1.sh

バックグラウンドで複数のコマンドを実行する

nohupコマンドを使用すると、バックグラウンドで複数のコマンドを実行できます。次の例では、mkdir、touch、およびlsコマンドが、nohupおよびbashコマンドを使用してバックグラウンドで実行されます。

$ nohup bash -c 'mkdir TestDir && touch test2.file && ls'> output.txt
nohup: ignoring input and redirecting stderr to stdout

$ cat output.txt 
TestDir
nohup.out
output.txt
test2.file
testfile.txt

バックグラウンドでのプロセスの開始と終了

nohupを使用してpingコマンドを実行した場合、ターミナルを閉じてもプロセスは終了しません。

$ nohup ping -i 15 linoxide.com &
[1] 806
$ nohup: ignoring input and appending output to 'nohup.out'

ターミナルを閉じて、再度開くことができます。次に、-aを指定してpgrepコマンドを実行します オプション。

$ pgrep -a ping
816 ping -i 15 linoxide.com

実行中のプロセスIDを持つプロセスのリストが表示されます。ご覧のとおり、実行したままのpingコマンドは終了していません。バックグラウンドプロセスを終了するには、killコマンドを実行する必要があります。パラメータとして、実行中の特定のプロセスIDを使用する必要があります。

この場合、プロセスIDは816です。kill816を実行して、プロセスを終了します。

$ pgrep -a ping
816 ping -i 15 linoxide.com
$ kill 816

結論

Nohupは、シェルスクリプトを実行する必要がある場合に非常に役立ちます。 Tmux、Screen、Disownは、nohupと同じ仕事をする代替ユーティリティの一部です。

多くの場合に役立つこのツールに慣れていることを願っています。ご質問やご意見がございましたら、以下に投稿してください。


Linux
  1. 例を含むLinuxTeeコマンド

  2. 例を含むLinuxヘッドコマンド

  3. LinuxでのJQコマンドと例

  1. 例を含むwcLinuxコマンド

  2. Linuxのソートコマンドと例

  3. Linuxでのエコーコマンド(例付き)

  1. Linuxでのmanコマンドと例

  2. LinuxでのAWKコマンドと例

  3. LinuxでのCurlコマンドと例