File:  [LON-CAPA] / loncom / init.d / loncontrol
Revision 1.38: download - view: text, annotated - select for diffs
Mon Jul 6 00:07:15 2009 UTC (14 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_9_99_0, version_2_8_99_0, bz6209-base, bz6209, bz2851, HEAD, GCI_3, GCI_2
- Pass $iptables (path to iptables) as arg to &get_fw_chain().

    1: #!/usr/bin/perl
    2: #
    3: # $Id: loncontrol,v 1.38 2009/07/06 00:07:15 raeburn Exp $
    4: #
    5: # The LearningOnline Network with CAPA
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: # Startup script for the LON-CAPA network processes
   30: #
   31: 
   32: # chkconfig: 345 95 5
   33: # description: LON-CAPA is a "network of knowledge".  It is used to \
   34: # distribute knowledge resources and instructional management.
   35: # processnames: lonc, lond, lonsql, lonmaxima, lonr
   36: # pidfiles: /home/httpd/perl/logs/lon*.pid
   37: # config: /etc/httpd/conf/loncapa.conf
   38: # config: /home/httpd/lonTabs/hosts.tab
   39: # config: /home/httpd/lonTabs/spare.tab
   40: # SuSE chkconfig/insserv info
   41: ### BEGIN INIT INFO
   42: # Provides:       loncapa
   43: # Required-Start: mysql apache2 $network $remote_fs
   44: # Required-Stop:
   45: # Default-Start:  3 4 5
   46: # Default-Stop:
   47: # Description:    Starts the LON-CAPA services
   48: ### END INIT INFO
   49: 
   50: use strict;
   51: use lib '/home/httpd/lib/perl/';
   52: use LONCAPA::Configuration;
   53: use LONCAPA::Firewall;
   54: use Apache::lonnet;
   55: 
   56: my $command=$ARGV[0]; $command=~s/[^a-z]//g;
   57: 
   58: $ENV{'PATH'}="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin";
   59: $ENV{'BASH_ENV'}="";
   60: 
   61: sub stop_daemon {
   62:     my ($daemon,$killallname)=@_;
   63:     my $pidfile="/home/httpd/perl/logs/$daemon.pid";
   64:     
   65:     printf("%-15s ",$daemon);
   66:     if (-e $pidfile) {
   67: 	open(PIDFILE,$pidfile);
   68: 	my $daemonpid=<PIDFILE>;
   69: 	chomp($daemonpid);
   70: 	kill TERM => $daemonpid;
   71: 	my $count=0;
   72: 	while ($count++ < 5 && kill(0 => $daemonpid)) {
   73: 	    sleep 1;
   74: 	}
   75: 	if (kill 0 => $daemonpid) {
   76: 	    kill KILL => $daemonpid;
   77: 	    sleep 1;
   78: 	    if (kill 0 => $daemonpid) {
   79: 		print("failed to kill");
   80: 	    } else {
   81: 		print("killed");
   82: 	    }
   83: 	} else {
   84: 	    print("stopped");
   85: 	}
   86:     } else {
   87: 	print("not running");
   88:     }
   89:     system("killall -q -0 $killallname");
   90:     if ($? == 0) {
   91: 	system("killall -q $killallname");
   92: 	print(", killed off extraneous processes");
   93:     }
   94:     unlink($pidfile);
   95:     print("\n");
   96: }
   97: 
   98: sub clean_sockets {
   99:     opendir(SOCKETS,"/home/httpd/sockets/");
  100:     my $perlvarref=&LONCAPA::Configuration::read_conf();
  101:     return if (ref($perlvarref) ne 'HASH');
  102:     while (my $fname=readdir(SOCKETS)) {
  103: 	next if (-d $fname
  104: 		 || $fname=~/(mysqlsock|maximasock|\Q$perlvarref->{'lonSockDir'}\E)/);
  105: 	unlink("/home/httpd/sockets/$fname");
  106:     }
  107: }
  108: 
  109: if ($command eq "restart") {
  110:     print 'Restarting LON-CAPA'."\n";
  111:     print 'Ending LON-CAPA client and daemon processes'."\n";
  112:     foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima','lonr') {
  113: 	my $killallname=$daemon;
  114: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  115: 	&stop_daemon($daemon,$killallname);
  116:     }
  117:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  118: 	"\n";
  119:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  120: } elsif (($command eq "stop") || ($command eq 'start') || ($command eq 'status')) {
  121:     my $iptables = &LONCAPA::Firewall::get_pathto_iptables();
  122:     my $fw_chain = &LONCAPA::Firewall::get_fw_chain($iptables);
  123:     my $lond_port = &LONCAPA::Firewall::get_lond_port();
  124:     my %iphost = &Apache::lonnet::get_iphost();
  125:     if ($command eq 'stop') {
  126:         print 'Stopping LON-CAPA'."\n";
  127:         foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima','lonr') {
  128: 	    my $killallname=$daemon;
  129: 	    if ($daemon eq 'lonc') { $killallname='loncnew'; }
  130: 	    &stop_daemon($daemon,$killallname);
  131:         }
  132:         my $firewall_result = 
  133:             &LONCAPA::Firewall::firewall_close_port($iptables,$fw_chain,$lond_port,[$lond_port]);
  134:         if ($firewall_result) {
  135:             print "$firewall_result\n";
  136:         }
  137:         &clean_sockets();
  138:     } elsif ($command eq "start") {
  139:         my $firewall_result = 
  140:             &LONCAPA::Firewall::firewall_open_port($iptables,$fw_chain,$lond_port,\%iphost,[$lond_port]);
  141:         if (($firewall_result eq 'ok') || ($firewall_result eq 'inactive firewall')) {
  142:             if ($firewall_result eq 'inactive firewall') {
  143:                 print "WARNING: iptables firewall is currently inactive\n";
  144:             }
  145:             print 'Starting LON-CAPA'."\n";
  146:             print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  147: 	          "\n";
  148:             system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  149:         } else {
  150:             print "Not starting LON-CAPA\n";
  151:             if ($firewall_result eq 'port number unknown') {
  152:                 print "Could not check for status of LON-CAPA port in running firewall - port number unknown.  \n";
  153:             } elsif ($firewall_result) {
  154:                 print "$firewall_result\n";
  155:             }
  156:         }
  157:     } elsif ($command eq "status") {
  158:         my $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
  159:         if ($response=~/No such file or directory/) {
  160: 	    print 'LON-CAPA is not running.'."\n";
  161:         } else {
  162: 	    print 'LON-CAPA is running.'."\n";
  163: 	    system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
  164:         }
  165:         if (! &LONCAPA::Firewall::firewall_is_active()) {
  166:             print 'The iptables firewall is not active'."\n";
  167:         }
  168:         if ($lond_port) {
  169:             if (&LONCAPA::Firewall::firewall_is_port_open($iptables,$fw_chain,$lond_port,$lond_port,\%iphost)) {
  170:                 print "The LON-CAPA port ($lond_port) is open in firewall.\n";
  171:             } elsif (&LONCAPA::Firewall::firewall_is_active) {
  172:                 print "The LON-CAPA port ($lond_port) is NOT open in running firewall!\n";
  173:             }
  174:         } else {
  175:             if (&LONCAPA::Firewall::firewall_is_active()) {
  176:                 print "Could not check for status of LON-CAPA port in running firewall - port number unknown.\n";
  177:             } else {
  178:                 print "LON-CAPA port number is unknown, and firewall is not running.\n";
  179:             }
  180:         }
  181:     }
  182: } elsif ($command eq "reload") {
  183:     print 'Reload LON-CAPA config files'."\n";
  184:     system("su www -c '/home/httpd/perl/loncron --justreload'");
  185: } else {
  186:     print "You need to specify one of reload|restart|stop|start|status on the command line.\n";
  187: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>