AppleScript C Perl Shell Xcode Other

Keep remote server processes up and running

Post Reply
coding / perl     Views: 49Prev .. Next
Keep remote server processes up and runningPosted: Friday, March 22, 2024 [21:10:40] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 357
Sometimes virtual hosting service kill processes without letting users know.
Manually checking is time consuming and requires personally checking them.
Automating the process is quite easy with a few lines of code:
Keep remote server processes up and running
View Code#!/env/perl
# Program run every hour. Password-less access has to be set on server
# Comment and uncomment processes related to your server setup
use strict;
use warnings;
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local:/usr/local/bin:/opt/local/bin';

## Monthly rotate the log
unless(-f '/logs/server.rotated') {
    `touch /logs/server.rotated`;
    if(-f '/prog/check.eu.server.log.txt') {`mv /prog/check.server.log.txt /prog/check.server.log.old.txt`;}
}
    my $nowd=time;
    my $finaltime = localtime($nowd);
    $finaltime =~ tr/ //s;
my($memcached,$redis,$apache,$nginx,$sudo);
# Add services that are installed on your server. Full path would work best for remote commands
    $sudo='/usr/local/bin/sudo';
    $memcached='/usr/sbin/service memcached start';
#    $redis='/usr/local/etc/rc.d/redis start';
#    $apache='/usr/sbin/service apache22 start';
    $nginx='/usr/local/etc/rc.d/nginx start';
## Write the log
open(STDOUT,">>/prog/check.server.log.txt");
print "$finaltime\n";
my $server = 'user@IP_ADDRESS';    # Regular user to check the processes
my $suserver = 'sudo_user@IP_ADDRESS';    # sudo user to actually restart the processes
    my $serverresult = `ssh $server '/bin/ps axw'`;
my $doprint='';

if($memcached) {
    unless($serverresult =~ m/memcached/) {
        print "\nNo \"memcached\" process found - restarting it\n";
        my $resmm = `ssh $suserver '$sudo $memcached'`;
        print "Result: $resmm\n";    $doprint="Memcached restart:\n$resmm";
    }
}

if($redis) {
    unless($serverresult =~ m#redis-server#) {
        print "\nNo \"redis\" process found - restarting it\n";
        my $resrd = `ssh $suserver '$sudo $redis'`;
        print "Result: $resrd\n";    $doprint.="\nRedis restart: $resrd";
    }
}

if($apache) {
    unless($serverresult =~ m#sbin/httpd#){
        print "\nNo \"Apache\" process found - restarting it\n";
        my $resap = `ssh $suserver '$sudo $apache'`;
        print "Result: $resap\n";    $doprint.="\nApache restart: $resap";
    }
}

if($nginx) {
    unless($serverresult =~ m#nginx#) {
        print "\nNo \"Nginx\" process found - restarting it\n";
        my $resng = `ssh $suserver '$sudo $nginx'`;
        print "Result: $resng\n";    $doprint.="\nNGINX restart: $resng";
    }
}

if($doprint) {
print "\nInitial 'ps axw' result:\n$serverresult\n";
sendresults($serverresult,$doprint); # Reporting is optional
} else {
print "No stopped processes found\n";
}

print "\n-----------------------------\n";
close(STDOUT);
exit(0);

sub sendresults { ########
my($psaxw,$smres)=@_;
my $sendmail = '/usr/sbin/sendmail -t -oi';
open(MAIL,"|$sendmail");
print MAIL <<EOM;
From: user\@your_box
To: to_user\@domain
Subject: Server Error(s) report
\nReported by /user/home/check.server.cgi at: $finaltime
While checking server the following error(s) occurred and the following services restarted:
$smres
ps axw result:
$psaxw
EOM
close(MAIL);
} ## END SEND RESULTS ####

It can be run as a cron jobThere's no place like ~
coding / perlPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other
Our Telegram Group