Linuxにおけるプロセス管理のコマンド

この記事では、Linuxにおけるプロセス管理について説明します。Linuxにおけるプロセスとは、実行中のプログラムのことです。プログラムの実行中のインスタンスです。実行する任意のコマンドはプロセスを開始します。

Linuxにおけるプロセスの種類

Linuxにおいて、プロセスは2つのタイプに分けられます。

  • Foreground Processes
    depend on the user for input
    also referred to as interactive processes
  • Background Processes
    run independently of the user
    referred to as non-interactive or automatic processes

Linuxにおけるプロセスの状態

Linuxにおけるプロセスは、作成された後、終了する前にさまざまな状態を経ることがあります。これらの状態は次の通りです。

  • Running
  • SleepingInterruptible sleep
    Uninterruptible sleep
  • Stopped
  • Zombie
  • A process in running state means that it is running or it’s ready to run.
  • The process is in a sleeping state when it is waiting for a resource to be available.
  • A process in Interruptible sleep will wakeup to handle signals, whereas a process in Uninterruptible sleep will not.
  • A process enters a stopped state when it receives a stop signal.
  • Zombie state is when a process is dead but the entry for the process is still present in the table.

Linuxのプロセス管理における異なるコマンド

Linuxで実行中のプロセスを追跡するためには、2つのコマンドが利用可能です。これらのコマンドは「Top」と「Ps」です。

1. Linuxプロセスを管理するためのトップコマンド

マシン上で実行中のプロセスを追跡するには、topコマンドを使用することができます。

$ top 
Top

Topコマンドは、実行中のプロセスのリストをリアルタイムで表示し、それぞれのメモリとCPU使用率も表示します。出力内容をより理解しましょう。

  • PID: Unique Process ID given to each process.
  • User: Username of the process owner.
  • PR: Priority given to a process while scheduling.
  • NI: ‘nice’ value of a process.
  • VIRT: Amount of virtual memory used by a process.
  • RES: Amount of physical memory used by a process.
  • SHR: Amount of memory shared with other processes.
  • S: state of the process‘D’ = uninterruptible sleep
    ‘R’ = running
    ‘S’ = sleeping
    ‘T’ = traced or stopped
    ‘Z’ = zombie
  • %CPU: Percentage of CPU used by the process.
  • %MEM; Percentage of RAM used by the process.
  • TIME+: Total CPU time consumed by the process.
  • Command: Command used to activate the process.

リストを上下にナビゲートするために上下の矢印キーを使用できます。終了するには「q」を押してください。プロセスを終了するには、上下の矢印キーでプロセスを選択し、「k」を押してください。

後で見ることができる「killコマンド」を使用することもできます。

2. psコマンド → psコマンド

psコマンドは、『プロセス状態』の略称で、現在実行中のプロセスを表示します。ただし、topコマンドとは異なり、生成される出力はリアルタイムではありません。

$ ps
Ps Command

用語は次のようになります:

PID process ID
TTY terminal type
TIME total time the process has been running
CMD name of the command that launches the process

psコマンドを使用してさらに情報を取得する。

$ ps -u
Ps U

ここに: (Koko ni)

  • %CPU represents the amount of computing power the process is taking.
  • %MEM represents the amount of memory the process is taking up.
  • STAT represents process state.

psコマンドは現在実行中のプロセスのみを表示しますが、すべてのプロセスをリストするためにも使用することができます。

$ ps -A 
Ps A

現在実行されていないプロセスを含めて、このコマンドはすべてのプロセスをリストアップします。

3. プロセスを停止する (Purosesu wo teishi suru)

Linuxでプロセスを停止するには、’kill’コマンドを使用します。killコマンドはプロセスにシグナルを送信します。

異なる種類のシグナルを送信することができます。しかし、最も一般的なものは「kill -9」であり、それは「SIGKILL」と呼ばれています。

次のようにして、すべての信号をリスト表示することができます。

$ kill -L
Kill L

デフォルトのシグナルは15であり、それはSIGTERMです。つまり、番号を指定せずに単にkillコマンドを使用すると、SIGTERMシグナルが送信されます。

プロセスを終了するための構文は以下の通りです:

$ kill [pid]

代わりに、あなたはまた次のようにも使用することができます:

$ kill -9 [pid]

このコマンドはプロセスに「SIGKILL」シグナルを送信します。通常のキル要求を無視する場合に使用するべきです。

4. プロセスの優先度を変更する

Linuxでは、プロセス間の優先順位を設定することができます。プロセスの優先度値は「ニース(Niceness)」値と呼ばれます。ニース値は-20から19までの範囲で指定されます。デフォルトの値は0です。

topコマンドの出力の4番目の列は、niceness値の列です。

Niceness

デフォルト以外の値でプロセスを開始するには、次のように使用します。 (Deforuto igai no kachi de purosesu o kaishi suru ni wa, tsugi no yō ni shiyō shimasu.)

$ nice -n [value] [process name]

既に実行中のプロセスの優先度を変更するには、次の方法を使用します。

renice [value] -p 'PID'

結論は、

このチュートリアルでは、Linuxのプロセス管理について説明しました。主に、プロセス管理の実践的な側面に焦点を当てました。理論的には、プロセス管理は広範なトピックであり、完全にカバーすることはこのチュートリアルの範囲外です。

コメントを残す 0

Your email address will not be published. Required fields are marked *