0

私は bash が初めてで、コマンドの出力の解析について質問があります。同じ名前の「プロセス」を持つ3つのプロセスがあり、プロセスにはいくつかのパラメーターがあります。次に例を示します。

 process -a 10 -b 20 -c 30 ...
 process -a 15 -b 30 -c 40 ...
 process -a 30 -b 40 -c 50 ...

プロセスが存在する場合は、「a」パラメーターを処理して配列に割り当てる必要があります。それらが存在しない場合は、プロセスを再起動する必要があります。私は次のプロセスを処理しています:

 `$PS -ef|$GREP -v grep|$GREP process`

これにより、実行中のプロセスが表示されます。実行されていないプロセスを確認し、「a」パラメーターを使用して再起動する必要があります。

どうすればこれを達成できますか?

4

3 に答える 3

0
in_array() { for v in "${@:2}"; do [[ "$v" = "$1" ]] && return 0; done; return 1; }

relaunch () {
    echo "Do whatever you need to do in order to run again with $1"
}

watch=(10 15 30)
running=()
while read -r proc a av b bv c cv ; do
    printf 'a was %s, b was %s, c was %s\n' "$av" "$bv" "$cv" # can be omitted
    running=("${running[@]}" "$av")
done < <(ps -C process -o cmd=)

for item in "${watch[@]}" ; do
    in_array "$item" "${running[@]}" || relaunch "$item"
done
于 2012-07-10T10:46:01.393 に答える
0

ウォッチャー.sh:

#!/bin/bash

pid=$(pgrep -f 'process $1 $2')
[[ -z $pid ]] || wait $pid

echo "process $@: restarted"
process $@
exec $0 $@

プロセスごとに独自のウォッチャーを開始します。

nohup ./watcher.sh -a 10 -b 20 -c 30 ... &
nohup ./watcher.sh -a 15 -b 30 -c 40 ... &
nohup ./watcher.sh -a 30 -b 40 -c 50 ... &
于 2012-11-12T09:53:24.443 に答える
0

プロセスを開始して監視するラッパースクリプトを作成できます。

一般的な流れになります。

var pid_of_bg_process

func start_process
 process -a 10 -b 20 -c 30 ... &
 pid_of_bg_process=$!

start_process    
while true
 sleep 1min
 if file not exists /proc/$pid_of_bg_process
  alert_process_being_restarted
  start_process
 else
  continue
于 2012-07-10T08:41:55.843 に答える