LoginSignup
3
0

More than 5 years have passed since last update.

プロセスをチェックするのにpsからtopに変更してみた

Last updated at Posted at 2018-11-05

プロセスの状況を表示する

バッチ処理メインのサーバで、ジョブの実行状態をチェックするのにpsコマンドで検索することが多いので、grepを組み合わせたwrapperシェルを作成してみた。

psコマンドはその時のCPU使用率を表示していない

色々調べてると、「psコマンドで表示されるCPU使用率はプロセスの生存期間中に利用した時間のパーセンテージで表される、負荷を見るには適切で無い。負荷を見るにはtopコマンドを使用する。」とあったので、topコマンドも(ほぼ)同時に表示するようにしました。

[rookie@host001 ~]$ psgrep.sh ntpd
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
ntp       2444  0.0  0.0  26520  1276 ?        Ss   Oct21   0:45 ntpd -x -u ntp:ntp -p /var/run/ntpd.pid -g
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                 
 2444 ntp       20   0 26520 1276 1112 S  0.0  0.0   0:45.32 ntpd -x -u ntp:ntp -p /var/run/ntpd.pid -g                                                                                              
[rookie@host001 ~]$ 

項目の並びが異なっていて見にくいですね。
awk等でちまちま並びを合わせようと思いましたが、

topコマンドの結果だけでいいやん

てことで、以下の通りにしました。

psgrep.sh
MOJIMOJI=$1
### top の結果だけで十分なので、psの結果は表示中止
### echo "*** ps aux | grep ${MOJIMOJI} *** %CPU = プロセスが起動してからどの程度CPUを使っているかの割合"
### ps aux        | grep -E "(${MOJIMOJI}|%CPU)" | grep -v grep
echo "*** top -c | grep ${MOJIMOJI} *** %CPU = タスクの所要 CPU 時間の占有率。総 CPU 時間のパーセンテージ(ps auxより正確)"
top -b -c -n 1| grep -E "(${MOJIMOJI}|%CPU)" | grep -v grep
echo "*** command end   ${MOJIMOJI} ***"

exit $?
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0