Showing posts with label deploy. Show all posts
Showing posts with label deploy. Show all posts

Sunday, September 1, 2013

Restart Tomcat Service

Restart Tomcat Service with cleaned webapps directory

Create power-shell script file with below script (say "serviceRestarter.ps1")



# *******************************************************************************
# Use of this script is
# path_to_script_file ServiceName="" catalinaHome="" appName=""
# SERVICE_NAME is the name of the service to restart
# CATALINA_HOME is absolute path of catalina_home
# APP_NAME is the name of the war file without extension
#
# @author Amit Nema
#
# *******************************************************************************

#parameters to pass with script
param(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$ServiceName,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$catalinaHome,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$appName
)

# type the name of the service in the quotes here
$service = Get-Service -Name $ServiceName

#Stoping Service
Stop-Service $ServiceName
$service.WaitForStatus('Stopped','00:10:00')
if ($service.Status -ne 'Stopped') { 
Write-Warning 'Something is fishy here...'
exit
} Else {
write-host 'Service ' $ServiceName ' is ' $service.Status ' now.'
}
sleep 10

#Delete the content of web-apps
write-host 'Removing application :' $catalinaHome'\webapps\'$appName '.'
Remove-Item -Recurse -Force $catalinaHome'\webapps\'$appName
Remove-Item -Force $catalinaHome'\webapps\'$appName'.war'
Remove-Item -Recurse -Force $catalinaHome'\work\Catalina\localhost\'$appName
Remove-Item -Recurse -Force $catalinaHome'\work\Catalina\localhost\_'
sleep 10

#Starting Service
Start-Service $ServiceName
$service.WaitForStatus('Running','00:10:00')
if ($service.Status -ne 'Running') { 
Write-Warning 'Something is fishy here...' 
} Else {
write-host 'Service ' $ServiceName ' is ' $service.Status ' now.'
}
sleep 10