Connecting Tech Pros Worldwide Help | Site Map

Starting and stopping service

Newbie
 
Join Date: Sep 2008
Posts: 15
#1: Feb 10 '09
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(hostName, serviceName) and StopService(hostName, 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's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#2: Feb 10 '09

re: Starting and stopping service


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 ]
Newbie
 
Join Date: Sep 2008
Posts: 15
#3: Feb 10 '09

re: Starting and stopping service


Expand|Select|Wrap|Line Numbers
  1. sub StartServ {
  2.  
  3.      $computername = '';
  4.      $servicename = 'Apache Tomcat Server';
  5.  
  6.      Win32::Service::GetStatus("\\\\".$computername, $servicename, \%status);
  7.      die "service is arealdy started\n" if ($status{CurrentState} == 4);
  8.  
  9.      Win32::Service::StartService("\\\\".$computername,$servicename) || die "Can't start service\n";
  10.      print "Service started\n";
  11. }
  12.  
  13.  

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?
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#4: Feb 10 '09

re: Starting and stopping service


Set:
Expand|Select|Wrap|Line Numbers
  1.    $computername = 'localhost'; 
  2.  
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.
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#5: Feb 10 '09

re: Starting and stopping service


Have you even tried using the system commands with backticks

eg.

Expand|Select|Wrap|Line Numbers
  1.  system(`net start Apache Tomcat Server`); 
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,565
#6: Feb 10 '09

re: Starting and stopping service


Quote:

Originally Posted by Icecrack View Post

Have you even tried using the system commands with backticks

eg.

Expand|Select|Wrap|Line Numbers
  1.  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.
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#7: Feb 11 '09

re: Starting and stopping service


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 :)
Newbie
 
Join Date: Sep 2008
Posts: 15
#8: Feb 11 '09

re: Starting and stopping service


Hi,

I have tried your suggestions. The

Expand|Select|Wrap|Line Numbers
  1.   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.
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#9: Feb 11 '09

re: Starting and stopping service


Quote:

Originally Posted by TeenaRoz View Post

Hi,

I have tried your suggestions. The

Expand|Select|Wrap|Line Numbers
  1.   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

Expand|Select|Wrap|Line Numbers
  1.   system("net start Apache\ Tomcat\ Server");  
Newbie
 
Join Date: Sep 2008
Posts: 15
#10: Feb 11 '09

re: Starting and stopping service


This seemed to have worked.

Expand|Select|Wrap|Line Numbers
  1.  $servicename = 'net start "Apache Tomcat Server"';
  2. system("$servicename");
Thanks for all your suggestions. Really Appreciate it.
Reply