Luis Diaz
asked on
Batch: check service status
Hello experts,
I use the following script reported at:
http://stackoverflow.com/questions/3325081/how-to-check-if-a-service-is-running-via-batch-file-and-start-it-if-it-is-not-r
In order to check the status of an specific service (Tomcat7 in that case) if the service is Running exit the sub (nothing to do) else restart the reported service.
I would like to boost the service.
By adding the following:
Generate a servicename.log file in the same location of the script in which I am going to report the execution date (date stamp: YYYY-MM-DD-HH-SS) of the script and the various related actions:
Example of action:
First case:
The service is already running
example of log:
2016-08-01-15-29-42 OK "Service" name is running, start action was'nt performed
or
Second case:
The service is not running:
2016-08-01-15-29-42 KO "Service"is off, start action was performed.
2016-08-01-15-29-4 Start action has finished.
2016-08-01-15-29-4 Rechecking the status
2016-08-01-15-29-42 OK "Service" name is running, start action was'nt performed
As you can see if the the reported service was reported as not running a double loop with the same action should be performed in order to be sure that the service is running right after the start action.
If you have questions, don't hesitate to contact me.
Thank you again for your help.
I use the following script reported at:
for /F "tokens=3 delims=: " %%H in ('sc query "Tomcat7" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
net start "Tomcat7"
)
)
pause
http://stackoverflow.com/questions/3325081/how-to-check-if-a-service-is-running-via-batch-file-and-start-it-if-it-is-not-r
In order to check the status of an specific service (Tomcat7 in that case) if the service is Running exit the sub (nothing to do) else restart the reported service.
I would like to boost the service.
By adding the following:
Generate a servicename.log file in the same location of the script in which I am going to report the execution date (date stamp: YYYY-MM-DD-HH-SS) of the script and the various related actions:
Example of action:
First case:
The service is already running
example of log:
2016-08-01-15-29-42 OK "Service" name is running, start action was'nt performed
or
Second case:
The service is not running:
2016-08-01-15-29-42 KO "Service"is off, start action was performed.
2016-08-01-15-29-4 Start action has finished.
2016-08-01-15-29-4 Rechecking the status
2016-08-01-15-29-42 OK "Service" name is running, start action was'nt performed
As you can see if the the reported service was reported as not running a double loop with the same action should be performed in order to be sure that the service is running right after the start action.
If you have questions, don't hesitate to contact me.
Thank you again for your help.
Not really answering your question, but ... did you know that most of what you are wanting to do is already in Services > Recovery
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much for this proposal I will test it tomorrow.
ASKER
Thank you very much for this proposal.
I tested and I have few remarks.
->When the service is not running, the script is able to properly identify the action however it doesn't start the service right after..
->The various actions are log in cmd promt however there is not .log file related to the various actions.
Do you there is a way to cover those requirements?
Thank you again for your help.
I tested and I have few remarks.
->When the service is not running, the script is able to properly identify the action however it doesn't start the service right after..
->The various actions are log in cmd promt however there is not .log file related to the various actions.
Do you there is a way to cover those requirements?
Thank you again for your help.
ASKER
Too fast I was not running the script as an administrator this is why the script was not able to start the
service however I have something strange.
When I launch the script as an administrator I have the following case:
If the service is running:
Nothing to say all is good.
If the service is not running I have the following:
I don't know why I don't get firstly the error line as in my previous comment and then I have the start action.
The .log file is not generated either when I launch the script as administrator.
Thank you again for your help.
service however I have something strange.
When I launch the script as an administrator I have the following case:
If the service is running:
Nothing to say all is good.
If the service is not running I have the following:
I don't know why I don't get firstly the error line as in my previous comment and then I have the start action.
The .log file is not generated either when I launch the script as administrator.
Thank you again for your help.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for this new version and for the remark concerning the log export, I haven't see the reported path variable, my mistake.
I took into account the new version and I don't why the following message
And with the previous version I don't have this message.
If I should install something I prefer in that case to keep the first proposal.
Thank you again for your advice.
I took into account the new version and I don't why the following message
And with the previous version I don't have this message.
If I should install something I prefer in that case to keep the first proposal.
Thank you again for your advice.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much it works.
Last question if for example I want to add an string to the log file name:
$LogFile = "C:\Temp\$($ServiceName).l og"
I would like to add "Monitoring_" & $($ServiceName).log"
How should I set up $LogFile variable?
Thank you again for your help.
Last question if for example I want to add an string to the log file name:
$LogFile = "C:\Temp\$($ServiceName).l
I would like to add "Monitoring_" & $($ServiceName).log"
How should I set up $LogFile variable?
Thank you again for your help.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works! Thank you again for your help!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!! !!!!!!!!!! !!