Jump to content

perl *NIX version of servicedaemon.pl


Monk

Recommended Posts

The original servicedaemon shell script is more Linux friendly than FreeBSD. commands like echo etc don't support -e -s flags, and there isn't a initlog function on FreeBSD. Also; some of the scripts are hard coded to /bin/bash which FreeBSD doesn't have by default, among other things.

 

I've rewritten most of the LinuxTemplates in perl since both Linux and FreeBSD have /usr/bin/perl

 

I plan on adding syslog() functions to write stuff to syslog from servicedaemon.pl

 

This is considered beta. I don't plan on color'izing the output

just yet, as you will need an external module. I think I could hack ANSI colors in printf though.

 


#!/usr/bin/perl

# Copyright (C) 2007 Gary Stanley <gary@velocity-servers.net>.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice(s), this list of conditions and the following disclaimer as
#    the first lines of this file unmodified other than the possible
#    addition of one or more copyright notices.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice(s), this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


require 5.008;
use POSIX;
use Switch;

my $DAEMONNAME =        "%serviceid%";
my $DAEMONDISPLAY =     "%displayname%";
my $MONOPATH =          "/home/tcadmin/mono";
my $SERVICESPATH =      "%installpath%/Monitor/Services";
my $EXECUTABLE  =       "TCAdminConsole.exe";
my $GAMEPIDFILE =       "$SERVICESPATH/$DAEMONNAME/current.pid";
my $USER = getlogin();


switch ($ARGV[0]) {
       case start { &start; }
       case stop { &stop; }
       case restart { &restart; }
       case status { &status; }
       else { print "Usage: $0 {start|stop|restart|status}"; }
}

sub start 
{
       printf "Starting $DAEMONDISPLAY: \n";
       # Read/Check PID
       my $pid="";
       my $retval = "$?";

       if (-e "$GAMEPIDFILE") 
       { 
               open(DAT, $pidfile) || die("ERROR: Could not pidfile!");
               @pid=<DAT>;
               close(DAT);
               chop($pid);
               if ( -e "/proc/$pid/status" )
               {
                       printf "$DAEMONDISPLAY is already running (pid: $pid).\n";
                       &failure;
                       return 1;
               }
       }
       chdir("$SERVICESPATH/$DAEMONNAME");
       $ENV{'LD_LIBRARY_PATH'} = '$MONOPATH/lib';
       if(! $USER == "tcadmin")
       {
               system("su","tcadmin","-m","-c","$MONOPATH/bin/mono","$SERVICEPATH/$EXECUTABLE > /dev/null 2>&1 &");
       } else {
               system("$MONOPATH/bin/mono","$SERVICEPATH/$EXECUTABLE","> /dev/null 2>&1 &");

       }

       if ($retval) 
       {
               &failure;
               printf "";
               return "$retval";
       } else {
               &success;
               printf "";
               return 0;

       }
}

sub stop 
{
       my $pid="";
       my $retval="";

       printf "Shutting down $DAEMONDISPLAY: \n";
       if (-e "$GAMEPIDFILE")
       {
               printf "$DAEMONDISPLAY is not running.\n";
               &failure;
               printf "";
               return 1;
       }

       open(DAT, $pidfile) || die("ERROR: Could not pidfile!");
       @pid=<DAT>;
       close(DAT);
       chop($pid);
       
       if ( -e "/proc/$pid/status" )
       {
               printf "$DAEMONDISPLAY is already running (pid: $pid).\n";
               &failure;
               return 1;
       }

       chdir("$SERVICESPATH/$DAEMONNAME");
       $ENV{'LD_LIBRARY_PATH'} = '$MONOPATH/lib';
       $ENV{'MONO_AOT_CACHE'} = 'True';

       system("touch","$SERVICESPATH/$DAEMONNAME/stop") || warn("ERROR: Couldn't write $SERVICESPATH/$DAEMONNAME/stop ");

               if(! $USER == "tcadmin")
       {
               system("su","tcadmin","-m","-c","$MONOPATH/bin/mono","$SERVICEPATH/$EXECUTABLE > /dev/null 2>&1 &");
       } else {
               system("$MONOPATH/bin/mono","$SERVICEPATH/$EXECUTABLE","> /dev/null 2>&1 &");
        
       }
               
       if ($retval)
       {
               sleep(%stopdelay%);
               &failure;
               printf "";
               return "$retval";
       } else {
               sleep(%stopdelay%);
               &success;
               printf "";
               return 0;

       }
}

sub restart 
{
       &stop;
       &start;
}
sub success {
       my $rc="$?";
       printf "$ARGV[0] \"$ARGV[1]\"";
       &print_success;
       return 0;
}

sub failure {
       my $rc="$?";
       printf "$ARGV[0] \"$ARGV[1]\"";  
       &print_failure;
       return $rc;
}

sub print_success {
       printf "[  ";
       printf "OK";
       printf "  ]";
       return 0;
} 

sub print_failure {
       printf "[  ";
       printf "FAILED";
       printf "  ]";
       return 1;
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Terms of Use