Starting and stopping service

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TeenaRoz
    New Member
    • Sep 2008
    • 15

    #1

    Starting and stopping service

    Hi Perl Experts,

    Is there any inbuilt sys functions which can start and stop a tomcat service in a given machine. I need to stop the service, copy a .war file into webapps folder and start the tomcat service. I can see that we have StartService(ho stName, serviceName) and StopService(hos tName, serviceName) which can be used. But when i try using it in a windows machine, i am getting errors saying unrecognized command. Can someone help in this regard.

    Thanks,
    Teena.
  • Icecrack
    Recognized Expert New Member
    • Sep 2008
    • 174

    #2
    if you are getting errors saying unrecognized command that means the command cant be found in windows also for us to understand this more, post your script in code tags

    [CODE] [/CODE ]

    Comment

    • TeenaRoz
      New Member
      • Sep 2008
      • 15

      #3
      Code:
      sub StartServ {
      
           $computername = '';
           $servicename = 'Apache Tomcat Server';
      
           Win32::Service::GetStatus("\\\\".$computername, $servicename, \%status);
           die "service is arealdy started\n" if ($status{CurrentState} == 4);
      
           Win32::Service::StartService("\\\\".$computername,$servicename) || die "Can't start service\n";
           print "Service started\n";
      }

      Here when this sub is called, the error message "Can't start service" is displayed. I read that if we dont specify a machine name , the local machine is taken by default. So i have left that empty. So i guess this sys function does not work in windows.. is there any alternative?

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Set:
        Code:
           $computername = 'localhost';
        If you leave out the hostname argument for GetStatus method, then local machine will be considered by default. But in your case, you are setting the hostname to null.

        Comment

        • Icecrack
          Recognized Expert New Member
          • Sep 2008
          • 174

          #5
          Have you even tried using the system commands with backticks

          eg.

          Code:
           system(`net start Apache Tomcat Server`);

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            Originally posted by Icecrack
            Have you even tried using the system commands with backticks

            eg.

            Code:
             system(`net start Apache Tomcat Server`);
            Isn't that a bit redundant? You need one or the other, but I have never seen anyone try using both at the same time.

            Comment

            • Icecrack
              Recognized Expert New Member
              • Sep 2008
              • 174

              #7
              the system with backticks waits for the process and when complete returns a value.

              don't know if just backticks will do the same.

              so blame my teacher for only teaching us one way :)

              Comment

              • TeenaRoz
                New Member
                • Sep 2008
                • 15

                #8
                Hi,

                I have tried your suggestions. The

                Code:
                  system(`net start Apache Tomcat Server`);

                does not work either. What intrigues me is that , Win32::Service: :StartService is supposed to be used for windows right.. And yes I also tried using localhost as the cumputer name.Thanks for your suggestions though.

                Comment

                • Icecrack
                  Recognized Expert New Member
                  • Sep 2008
                  • 174

                  #9
                  Originally posted by TeenaRoz
                  Hi,

                  I have tried your suggestions. The

                  Code:
                    system(`net start Apache Tomcat Server`);

                  does not work either. What intrigues me is that , Win32::Service: :StartService is supposed to be used for windows right.. And yes I also tried using localhost as the cumputer name.Thanks for your suggestions though.

                  Sorry i made an error try

                  Code:
                    system("net start Apache\ Tomcat\ Server");

                  Comment

                  • TeenaRoz
                    New Member
                    • Sep 2008
                    • 15

                    #10
                    This seemed to have worked.

                    Code:
                     $servicename = 'net start "Apache Tomcat Server"';
                    system("$servicename");
                    Thanks for all your suggestions. Really Appreciate it.

                    Comment

                    Working...