0

All I want from this script is to ssh to the host, and check if the process is alive, and if it is not, I want the littel script to die. Does not die though. It stops, and then starts up again on the ssh is successful again. I want death though.

#!/bin/bash
iterate=0
while [ $iterate -le 20000 ]
do
   rc=$?
   ssh -q -T coolhost "ps -ef | egrep '[i]cool-process' | grep wrapper  "
   if [[  $rc -eq 0 ]] ; then
       sleep 2
       iterate=$((iterate+1 ))
   else
       break
       exit 1
   fi
done

It will iterate to 2000, however if the remote process breaks, it will not die. It will not break and exit.

this will work - but won't sleep - if I put a sleep the rc goes to 0 and is never dies. so this works but is too basic.

#!/bin/bash
set -e
while : ; do
   ssh -q -T coolhost "ps -ef | egrep '[i]cool-process' | grep wrapper" > /dev/null 2>&1
done
2
  • 1
    Instead of ps -ef | grep ..., use pgrep. This is a much more direct way to search for processes by name (note useful options like -u and -f which you may want or need). It will return a failure code if the process is not found. Commented Jan 6, 2015 at 0:27
  • rc=$? should be on the line following ssh... Commented Jan 6, 2015 at 0:31

1 Answer 1

2

You set rc=$? before the ssh command, and the last command was the test ([) command, which just succeeded, so when you test if [[ $rc -eq 0 ]] the answer is always 'yes, it does'.

It's best to test the status of ssh directly:

#!/bin/bash
iterate=0
while [ $iterate -le 20000 ]
do
    if ssh -q -T coolhost "ps -ef | egrep '[i]cool-process' | grep wrapper"; then
        sleep 2
        ((iterate++))
    else
        break   # or exit 1
    fi
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.