script use min resource ( grep grep)

Hi
i wrote script use it as watchdog ( i mean it check another program (pooya) whenever that was killed (closed or crashed) it run another script (pooya_start.sh) to start it,
this script work fine and do the job for me , i need help of an expert to tell me (exact command) how to change this script use Minimum resource ( Cpu & memory) to work?

while sleep 1
do
  if ps x | grep -v grep | grep -c pooya >/dev/null
  then 
  echo "" 
  else
echo `date` "R" >> /tmp/Check_pooya.log
 /var/bin/pooya_start.sh start &
  fi
done

exit 0

1,( line 3) : i was read in other forum don't grep grep : how could change this to other way ( please wrote exact command for me)

2.( line 5) : is there any better command instead of echo "" use no memory and do nothing ( just wrote this line to do nothing if pooya is run)

I really appreciate any help
Best regards,

Which OS and shell are you using?

1 Like

thanks for reply
Stlinux (stapi)
Sh4
Linux version 2.6.23.17_stm23_A21-slobbie

I don't know what's Sh4, but if:

pgrep pooya

or

pgrep -f pooya

return the correct pid, you could try something like this:

while sleep 10; do
  pgrep pooya > /dev/null || {
    echo `date` R >> /tmp/Check_pooya.log
    /var/bin/pooya_start.sh start &
    }
done

---------- Post updated at 03:26 PM ---------- Previous update was at 03:25 PM ----------

Edit: && -> || :slight_smile:

1 Like

pgrep command is unknown in sh4
any other way please?
or any fix on my code to use minimum resource? (ie is that echo "" the best way ( use min resource) to do nothing

OK,
change:

pgrep pooya > /dev/null

to

ps -ef | grep '[p]ooya' > /dev/null
1 Like

works perfect, thanks
so it must be best one for min resource
Best wishes

What does the output from "ps x" look like? It is a syntax error on most versions of "ps".

You could do even better. When you start the process with /var/bin/pooya_start.sh start & , you could save the pid of the process:

echo $! > /tmp/pooya.pid

At the next iteration, you could simply run:

kill -0 "$(</tmp/pooya.pid)" || ... your restart code here ...
1 Like