Skip to content

Commit

Permalink
Create tomcat_service_restart.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
thrinathadevops authored Jun 22, 2024
1 parent aec62ae commit d281568
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Shell_Scripts/server_restart/tomcat_service_restart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash

# Configuration
SERVICE_NAME="tomcat"
LOG_FILE="/var/log/tomcat_service.log"

# Logging function
log_message() {
echo "$(date): $1" | tee -a $LOG_FILE
}

# Functions
get_tomcat_pid() {
echo $(ps -ef | grep '[o]rg.apache.catalina.startup.Bootstrap' | awk '{print $2}')
}

start_tomcat() {
log_message "Starting Tomcat..."
if sudo systemctl start $SERVICE_NAME; then
sleep 3 # Give it a moment to start
local pid=$(get_tomcat_pid)
if [[ -n $pid ]]; then
log_message "Tomcat started with PID $pid."
else
log_message "Failed to start Tomcat."
fi
else
log_message "Error executing systemctl start $SERVICE_NAME."
fi
}

stop_tomcat() {
local pid=$(get_tomcat_pid)
log_message "Stopping Tomcat with PID $pid..."
if sudo systemctl stop $SERVICE_NAME; then
log_message "Tomcat stopped."
# Wait for Tomcat to stop before restarting
while sudo systemctl is-active --quiet $SERVICE_NAME; do
sleep 1
done
else
log_message "Error executing systemctl stop $SERVICE_NAME."
fi
}

restart_tomcat() {
local old_pid=$(get_tomcat_pid)
stop_tomcat
log_message "Waiting 60 seconds before attempting to start Tomcat..."
sleep 60
start_tomcat
local new_pid=$(get_tomcat_pid)
log_message "Tomcat restarted. Old PID was $old_pid. Current PID is $new_pid."
}

# Main script
{
old_pid=$(get_tomcat_pid)
if ! sudo systemctl is-active --quiet $SERVICE_NAME; then
log_message "Tomcat is not running."
start_tomcat
else
log_message "Tomcat is running with PID $old_pid."
restart_tomcat
fi
} || {
log_message "An error occurred during the execution of the script."
}

0 comments on commit d281568

Please sign in to comment.