0

I'm new to bash and I have a question about parsing the output of the command. I have 3 processes that have the same name "process" and the process have some parameters, for example:

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

I have to handle the 'a' parameters and assign them to an array if the processes exist. İf they don't exist, I have to restart the process. I am handling the processes with:

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

This gives me the running processes and I have to see which process does not run and restart it with the help of 'a' parameter.

How can I achieve this?

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
barp
  • 6,489
  • 9
  • 30
  • 37

3 Answers3

0

you can write a wrapper script which would start the process as well as monitor it.

Generic flow will be.

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
tuxuday
  • 2,977
  • 17
  • 18
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
sorpigal
  • 25,504
  • 8
  • 57
  • 75
0

watcher.sh:

#!/bin/bash

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

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

and start own watcher for each process:

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 ... &
jan
  • 45
  • 1
  • 4