4

以下にもある単純なシェルスクリプトがあります。

#!/usr/bin/sh

echo "starting the process which is a c++ process which does some database action for around 30 minutes"
#this below process should be run in the background
<binary name> <arg1> <arg2>

exit

今私が欲しいのは、プロセスのステータス情報を監視して表示することです。私はその機能に深く入りたくありません。30分で処理が完了することがわかっているので、1分ごとに3.3%が完了していることをユーザーに示し、バックグラウンドで処理が実行されているかどうかもチェックし、最後に処理が完了したかどうかを表示したい完了したこと。

誰か私を助けてくれませんか?

4

3 に答える 3

3

あなたができる最善のことは、アプリケーションにある種のインストルメンテーションを入れて、実際の進行状況をwork items processed / total amount of work.

それができない場合は、実際に実行されている時間を参照できます。

過去に使用したもののサンプルです。ksh93 および bash で動作します。

#! /bin/ksh
set -u
prog_under_test="sleep"
args_for_prog=30

max=30 interval=1 n=0

main() {
    ($prog_under_test $args_for_prog) & pid=$! t0=$SECONDS

    while is_running $pid; do
        sleep $interval
        (( delta_t = SECONDS-t0 ))
        (( percent=100*delta_t/max ))
        report_progress $percent
    done
    echo
}

is_running() { (kill -0 ${1:?is_running: missing process ID}) 2>& -; }

function report_progress { typeset percent=$1
    printf "\r%5.1f %% complete (est.)  " $(( percent ))
}

main
于 2012-08-03T13:33:41.970 に答える
1

プロセスにパイプが含まれる場合、http://www.ivarch.com/programs/quickref/pv.shtmlが優れたソリューションになるか、代わりにhttp://clpbar.sourceforge.net/が使用されます。しかし、これらは本質的にプログレスバーを備えた「猫」のようなものであり、それらを通過する何かが必要です。コンパイルしてバックグラウンド プロセスとして実行し、終了時に終了できる小さなプログラムがあります。 http://www.dreamincode.net/code/snippet3062.htmプロセスが長時間実行されて終了した場合に、何かを 30 分間表示し、コンソールでほとんど完了したことを出力したい場合は、おそらく機能しますが、変更する必要があります。ループで数秒ごとに文字を表示し、前のプロセスのpidがまだ実行されているかどうかを確認する別のシェルスクリプトを作成するだけの方がよい場合があります.$$変数を見て親pidを取得できると思います/proc/pid でまだ実行されています。

于 2012-08-03T12:27:15.877 に答える
0

本当にコマンドに統計を出力させる必要がありますが、簡単にするために、プロセスの実行中にカウンターを単純にインクリメントするために、次のようなことを行うことができます。

#!/bin/sh

cmd &  # execute a command
pid=$! # Record the pid of the command
i=0
while sleep 60; do
  : $(( i += 1 ))
  e=$( echo $i 3.3 \* p | dc )   # compute percent completed
  printf "$e percent complete\r" # report completion
done &                           # reporter is running in the background
pid2=$!                          # record reporter's pid
# Wait for the original command to finish
if wait $pid; then
    echo cmd completed successfully
else
    echo cmd failed
fi      
kill $pid2        # Kill the status reporter
于 2012-08-03T19:02:09.543 に答える