[JDEV] Auto restarting jabberd

james rogers buckrogers1965 at go.com
Thu Feb 1 14:34:27 CST 2001


We were having a problem keeping the jabberd server running this week so I wrote a script to start jabberd and then login to the server every so often to make sure that it is responding.  If it isn't responding, the script kills jabberd and waits for four minutes to let everything settle down and then restarts jabberd...

This script is released under the GNU GPL and is released as is with no warrenty or implied functionallity.  It will not work unless you change some of the paths and user names to correspond to how you have jabber installed on your system.

run the script with the command:

nohup <scriptname> &

and the log will be saved to the nohup file in the current directory.

If you normally need to have any arguments to the script, add them after the jabberd command in the start_jabber function.

Good Luck!

---------Cut Here------------------

#!/PATH_TO_PERL/perl

use Socket;

while(1) {
    start_jabberd();
    sleep(60);
    while (connected()){
        sleep(60);
    }
    kill_jabberd();
    sleep(250);
}

sub start_jabberd {
    printf ("starting jabberd\n");

    # execute the command to run jabberd
    open(JAB, "cd /PATH_TO_EXECUTABLE; LD_LIBRARY_PATH=/PATH_TO_LIBARARY; ./jabberd |");
     close(JAB);

    return 0;
}

sub connected {

    # open a connection to jabber on the local server
    # if this fails then we return a 0

    $remote  = shift || 'localhost';
    $port    = shift || 5222;  # jabberd port
    $iaddr   = inet_aton($remote)               || return 0;
    $paddr   = sockaddr_in($port, $iaddr);

    $proto   = getprotobyname('tcp');
    socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || return 0;
    connect(SOCK, $paddr)    || return 0;

    # send a bogus stream string to server 
    # if this fails then we return a 0

    print SOCK "<stream:stream to='localhost' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>" || (close SOCK && return 0);

    # read stream responce from server
    $line = <SOCK>;

    # if response doesn't have a stream:steam in it, return a 0
    # Didn't seem to be needed, will implement latter only if required.

    # send a </stream:stream> sequence to close connection to jabberd server
    print SOCK "</stream:stream>" || (close SOCK && return 0);

    # close socket
    close (SOCK);

    return 1;
}

sub kill_jabberd {
    printf("stopping jabberd\n");

    # find PIDs of running jabberd servers owned by self
    open(JAB, "for i in `ps -ef|grep jabberd|grep foo | grep -v grep|awk '{print $2}'`; do echo $i; done |");
    close(JAB);

    return 0;
}

___________________________________________________
GO.com Mail                                    
Get Your Free, Private E-mail at http://mail.go.com






More information about the JDev mailing list