MrMystery
MrMystery

Reputation: 31

Writing a unix or perl script to check if a process is running

I have 4 app servers. I want to check that all 4 are up and running so I want to write a script that I check for the process on all app servers (without me having to log onto each one and run the script). The app servers have a share folder which they all have access to so I can place the script there. I want to find out the easiest/most efficient way to check this on all 4 app servers. The original plan was to ssh to each of the servers but this then asks you for your password and other prompts which will cause issues. I was looking into using keygen, but I don't have a home dir on the servers and I was having issues generating keys. I wanted to know if anyone could suggest the best way to do this via a script. Which would be better/more efficient, perl or unix? Are there other options of doing this that I am missing other than keygen? Any advise will be much appreciated.

Thanks.

Upvotes: 2

Views: 2035

Answers (4)

Seth Robertson
Seth Robertson

Reputation: 31451

Assuming you can get a script running, something like this would work.

#!/bin/sh
if [ $# -ne 1 ]; then echo "Usage $0: <hispidfile>" >&2; exit 1; fi
read otherpid < $1
if [ "$otherpid" -lt 1 ]; then
  otherpid=""
fi
while :; do
 while kill -0 $otherpid
  do
   sleep 1
  done
 # restart other program
 # (really restarting myself in my peer configuration)
 echo restarting him
 sh -c 'echo $$ > '$1'; echo Him is running for 5 seconds PID=`cat '$1'`; sleep 5;' &
 newpid=0
 while [ "$newpid" -eq "$otherpid" -o "$newpid" -lt 1 ]
  do
   sleep 2
   read newpid < $1
  done
 otherpid=$newpid
done

Upvotes: 0

user791953
user791953

Reputation: 639

I've done something similar using Perl. If you use Net:SSH:Expect, you can open a connection with a password into each machine, run your commands, check output and repeat.

Here's what'll get you started. CPAN - Net:SSH:Expect

Sample code to get inside of desired machine:

    my $ssh = Net::SSH::Expect->new(
        host     => $sshHost,
        password => 'password',
        user     => 'username',
        raw_pty  => 1
    );

my $login_output = $ssh->login(); # check if you logged in properly

# disable terminal translations and echo on the SSH server
$ssh->exec("stty raw -echo");

You can call exec to run whatever commands you need.

Upvotes: 1

wom
wom

Reputation: 436

without being able to setup ssh key's or a cron entry; my guess is rsh is out also. Off the top of my head; that leaves expect... http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/SSH/Expect.pod

Upvotes: 0

tdammers
tdammers

Reputation: 20721

You could run a cron job on each server that does the checking, and when the process isn't there, sends a message - could be an HTTP request, an e-mail, whatever you fancy.

Upvotes: 0

Related Questions