Script to verify SSH is running

Writing a simple test script that looks for ssh, kills if its running and verifies if its still running. If it isn't, move on. My issue, its cause I don't know how, is to verify if ssh is running still. Also, is there a way have this do this on a remote server? I already have the ssh keys working so all I need is to know how to code that part. below is what I have, help is much appreciated!

#!/sh/bin
check_stat=`ps -ef | grep sshd | grep -v grep | awk '{print $2}'`
if [ "${check_stat}X" != "X" ]
then
echo "SSHD is running"
else
echo "SSHD isn't running"
for pid in $check_stat
do
echo "Killing $pid"
kill -9 $pid
done
fi

If it is not running, you will not be able to ssh into the remote machine.

Why do you kill it if it isn't running?

check_stat=`ps -ef | grep 'shd' | awk '{print $2}'`
if [ -n "$check_stat" ]
then
   echo "SSHD is running"
   echo "Killing $check_stat"
   kill -9 $check_stat
else
   echo "SSHD isn't running"
fi



one way

while [ 1=1 ];
do
    pgrep sshd
    if [ $? -eq 1 ];then
      echo "sshd stopped"
      break
    fi
    echo "Still running"    
    pkill sshd
    sleep 2
done

This script is meant for an application, I'm only using SSHD as an example but you brought up a good point. I'll switch the sshd to ftpd and go from there.

So the new script is:
#!/sh/bin
check_stat=`ps -ef | grep vsftpd | grep -v grep | awk '{print $2}'`
if [ "${check_stat}X" != "X" ]
then
echo "vsftpd is running"
else
echo "vsftpd isn't running"
for pid in $check_stat
do
echo "Killing $pid"
kill -9 $pid
done
fi

'#!/bin/sh' != '#!/sh/bin'

There are a few problems with it:
Incorrect shebang

Unnecessary 2nd grep

Overly verbose test expression

Killing processes in the wrong section of the if command

Unnecessary for loop

#!/bin/sh
check_stat=`ps -ef | grep '[v]sftpd' | awk '{print $2}'`
if [ -n "$check_stat" ]
then
  echo "vsftpd is running"
  echo "Killing $check_stat"
  kill -9 $check_stat
done
else
  echo "vsftpd isn't running"
fi



root@s1 [/tmp]# ssh -v -p911 x.x.x.x ( where x.x.x.x is the IP of the remote machine and 911 is the ssh port on the remote machine)

For default port 22 ( you can simply use, ssh -v x.x.x.x )