2012-07-10 74 views
0

我是bash的新手,我有一个关于解析命令输出的问题。我有3个过程具有相同名称为“处理”,并且处理有一定的参数,例如:Bash脚本正则表达式

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` 

这给了我正在运行的进程,我要看看哪个进程不运行,并与'参数的帮助下重新启动它。

我该如何做到这一点?

+0

现在我们知道你想做的事,哪来的你的问题? – bos 2012-07-10 08:28:21

+0

@bos。我的问题是我怎么能做到这一点? – barp 2012-07-10 08:29:36

+0

您可以使用'ps -C process -o cmd ='来获得更好的命令及其参数列表。 – Sorpigal 2012-07-10 10:33:38

回答

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 
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 
0

watcher.sh:

#!/bin/bash 

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

echo "process [email protected]: restarted" 
process [email protected] 
exec $0 [email protected] 

,并开始为每个进程自己的守望者:

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 ... &