Bash | How to check tomcat is REALLY up / down - bash

Following script:
#Tomcat status
tc_status () {
ps aux | grep myapp123 | wc -l
}
#shutdown, if tomcat
if [ $(tc_status) -gt 0 ]; then
echo "Stopping Tomcat "; /home/user/shutdown.sh myapp123 > /dev/null 2>&1;
if [ $(tc_status) -eq 0 ]; then
echo "OK!"
fi
fi
#some other code
#startup
echo "Starting Tomcat...";
/home/user/startup.sh myapp123 > /dev/null 2>&1;
if [ $(tc_status) -gt 0 ]; then
echo "OK!";
fi
The above code first checks whether Tomcat is still running. If so, then it will be stopped, if not, further lines of code will be executed.
Finally the Tomcat is started. If it is running, there is an "OK" message.
The problem at this point is that the status is not 100% meaningful, especially when starting. Because the Tomcat process is running, but Tomcat usually takes several seconds or minutes until it is actually up.
How can I make sure that the Tomcat is actually DOWN and UP?

If your distro supports it, I recommend you to put your services under systemd. That way you can monitor it using the "service" command. It isn't hard to do it.
I know that this doesn't directly answers your question, but it'll give way more control over what's running and what isn't.

The unreliability of your script does not depend on the startup time of Tomcat, but on the fact that:
ps aux | grep app123 | wc -l
is unreliable: it matches also the grep app123 command (although this is subject to a race condition between ps and grep). On the other hand app123 is not a valid argument for the startup.sh script. The usage is:
startup.sh [ -config {pathname} ] [ -nonaming ] [ -generateCode [ {pathname} ] ] [ -useGeneratedCode ]
To reliably check if Tomcat is running, you should use a pidfile, which will be created at the location pointed by the CATALINA_PID environment variable.
So your script should look like:
# Be sure to have permissions to create this file
export CATALINA_PID=/run/tomcat.pid
# Tomcat status
tc_status () {
[ -f $CATALINA_PID ] && kill -0 $(<$CATALINA_PID)
}
#shutdown, if tomcat
if [ $(tc_status) -gt 0 ]; then
echo "Stopping Tomcat ";
/home/user/shutdown.sh >/dev/null 2>&1;
if [ $(tc_status) -eq 0 ]; then echo "OK!"; fi
fi
#some other code
#startup
echo "Starting Tomcat...";
/home/user/startup.sh >/dev/null 2>&1;
if [ $(tc_status) -gt 0 ]; then echo "OK!"; fi

Related

Script to detect and report if Service has changed started/stopped

I am trying to write a bash script that detects if a service has changed state. I have this so far:
while true; do
if [ -z "$(netstat -tulpn | grep 51827)" ];
then
echo notinuse
else
echo inuse
fi
sleep 5
done
It works but it will endlessly write when the service is up or down, and I only want it to report on the first instance of a state change. So report when the service was down, and then up as well as when it was up and then went down.
I started to create loop counters and comparing to a previous run but I got into a complete mess of variables. Can anyone help?
if [ -z "$(netstat -tulpn | grep 51827)" ];
then
echo notinuse
notify_status="notinuse-sent"
else
echo inuse
notify_status="inuse-sent"
fi
while true; do
if [ -z "$(netstat -tulpn | grep 51827)" ];
then
if [ $notify_status = "notinuse-sent" ]; then
echo "already notified"
else
echo notinuse
$notify_status="notinuse-sent"
fi
else
if [ $notify_status = "inuse-sent" ]; then
echo "already notified"
else
echo inuse
$notify_status="inuse-sent"
fi
fi
sleep 5
done

How to supply password from shell script without using expect command

I'm writing a bash script to stop start my postgres DB service. Initially I succeeded in creating one, but as soon I enabled SSL certificate it prompts to enter the phrase password.
I know the easiest solution is to use expect , but in my environment i am not authorized to use it.
Can someone help me out in scripting as to how can I supply the PEM PHRASE password without a user intervention.
This is what I have worked so far.
-bash-4.2$ cat start_postgres_db.sh
cd `dirname $0`
. `dirname $0`/parameter.env
${POSTGREBIN}/pg_ctl -D ${POSTGREDATAPATH} start -w
while true
do
sleep 1
loopcnt=0
loopcnt=`expr ${loopcnt} + 1`
PRCCNT=`ps -ef | grep ${DBEXENAME} | grep -v grep|wc -l`
if [ ${PRCCNT} -eq 1 ]
then
echo "PostgreSQL process started sucessfully"
exit
fi
if [ ${loopcnt} -gt 11 ]
then
echo "PostgreSQL process not started successfully"
echo "su to postgres and run ${POSTGREBIN}/pg_ctl -D ${POSTGREDATAPATH} restart"
exit
fi
done
Execution:
bash-4.2$ ./start_postgres_db.sh
waiting for server to start....Enter PEM pass phrase:.........
You can provide a password to pg_ctl as argument on the command line with the option -P. I will assume it is contained in the variable ${POSTGREPASSWORD}.
start_postgres_db.sh
cd `dirname $0`
. `dirname $0`/parameter.env
${POSTGREBIN}/pg_ctl start -w -D ${POSTGREDATAPATH} -P ${POSTGREPASSWORD}
while true; do
sleep 1
(( loopcnt++ ))
PRCCNT=$(ps -ef | grep ${DBEXENAME} | grep -v grep | wc -l)
if [ ${PRCCNT} -eq 1 ]; then
echo "PostgreSQL process started sucessfully"
exit 0
fi
if [ ${loopcnt} -gt 11 ]; then
echo "PostgreSQL process not started successfully"
echo "su to postgres and run ${POSTGREBIN}/pg_ctl -D ${POSTGREDATAPATH} restart"
exit 1
fi
done

OS X Bash Script: syntax error: unexpected end of file

I tried learning how to write a bash script to have my own script to start and stop a Tomcat Server, and I can't seem to find what is causing the error in this script. I've double checked my if and fi statements to make sure they match, but still have no idea what is wrong.
EDIT:
Here is the exact error message
line 87: syntax error: unexpected end of file
Here is the script:
#!/bin/bash
#Returns the process id of the Tomcat server if currently running
function tomcat_pid {
local pid=0
local temp=$(ps x | grep "$CATALINA_HOME" | grep -v grep | cut -d ' ' -f 1)
if [ -n "$temp" ]; then
pid=$temp
fi
echo "$pid"
}#tomcat_pid
#Checks the status of the Tomcat Server
function tomcat_status {
local retval="Tomcat Server is not currently running"
local pid
pid=$(tomcat_pid)
if [ "$pid" -gt 0 ]; then
retval="Tomcat Server is running at pid: $pid"
fi
echo "$retval"
}#tomcat_status
#Starts the Tomcat Server
function tomcat_start {
local pid
pid=$(tomcat_pid)
if [ "$pid" -gt 0 ]; then
echo "Tomcat Server already running at pid: $pid"
else
echo "Starting Tomcat Server"
"$CATALINA_HOME/bin/startup.sh"
fi
}#tomcat_start
#Stops the Tomcat Server
function tomcat_stop {
local pid
pid=$(tomcat_pid)
if [ "$pid" -gt 0 ]; then
echo "Shutting down Tomcat Server"
"$CATALINA_HOME/bin/shutdown.sh"
else
echo "Tomcat Server is not currently running"
fi
}#tomcat_stop
#Restarts the Tomcat Server
function tomcat_restart {
local pid
pid=$(tomcat_pid)
if [ "$pid" -gt 0 ]; then
echo "Restarting the Tomcat Server"
"$CATALINA_HOME/bin/shutdown.sh"
sleep 5s
"$CATALINA_HOME/bin/start.sh"
else
echo "Starting the Tomcat Server"
"$CATALINA_HOME/bin/startup.sh"
fi
}#tomcat_restart
if [ -n "$1" ]; then
if [ "$1" = 'restart' ]; then
tomcat_restart
#tomcat start - Starts the Tomcat Server
elif [ "$1" = 'start' ]; then
tomcat_start
#tomcat shutdown - Shuts down the Tomcat Server
elif [ "$1" = 'shutdown' ]; then
tomcat_stop
#tomcat status - Checks the status of the tomcat server
elif [ "$1" = 'status' ]; then
tomcat_status
else
echo "Please use correct options"
fi
else
echo "Please use correct options"
fi
See man bash, in other words bash(1), Section SHELL GAMMAR
{ list; }
list is simply executed in the current shell environment. list must be terminated with a newline or semi‐
colon. This is known as a group command. The return status is the exit status of list. Note that unlike
the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to
be recognized. Since they do not cause a word break, they must be separated from list by whitespace or
another shell metacharacter.
The last sentence points at your problem.

Shellscript - Check Running services

I'm new in Shellscript, and i'm getting some problems. I need a script to check if the services are running or not, if its not running, and dont exist the flag, start all services. What i'm doing wrong?
#!/bin/bash
file= "$PIN_HOME/apps/DE_BILL_MI_BRM/alarmistica/flags/intervencao.flag"
# Check if services are running
for service in $BRM_SERVICES
do
if [ps -ef | grep $service | grep -v grep | awk 'NR>1{for (i=1;i<=NF;i++)if($i==1) print "Services not running", i}' ]; then
echo $service " is not running correctly"
else
if [ -f "$file" ]; then
echo "Flag exists. The service will not start"
else
echo "$file not found. Starting all services"
pin_ctl start all
fi
fi
done
When ($i==1), the services is not running!
But the results is not corresponding. For exemple, when the services are down, the script dont start the services...
For checking process tables, use pgrep instead.
#!/bin/bash
file= "$PIN_HOME/apps/DE_BILL_MI_BRM/alarmistica/flags/intervencao.flag"
# Check if services are running
for service in $BRM_SERVICES
do
pgrep -f "$service";
exstat=$?; # This checks the exit status
if [ "$exstat" -eq 0 ] && ! [ -f "$file" ]; then
echo "pgrep returned exit status $extstat";
else
echo "$file not found. Starting all services"
pin_ctl start all
fi
done

Bash script - could someone proof read and explain why it is wrong

Тhis is the first script I've ever written and I'm looking for some help. I can't find a script like the one I'm trying to write and its becoming a bother because I wish to create more/expand upon such a bash structure.
If some one has the time could they maybe proof read this for me. Could someone tell me what am I doing wrong and why?
#! /bin/bash
# script to turn the screen blue
echo -e '\033[1;32m'
echo "Minecraft Server LTG Bukkit 1.4.7"
echo "Minecraft Server LTG Bukkit 1.4.7"
echo -e '\033[31m' "[Start ] \033[1;32m LTG MineCraft Server"
echo -e '\033[31m' "[Stop ] \033[1;32m LTG MineCraft Server"
echo -e '\033[31m' "[Restart] \033[1;32m LTG MineCraft Server"
echo -e '\033[0m'
cho -e "Hello, \033[47m \033[30m"$USER" \033[0m Enter Command:"
echo "Command:"
read $COM1 start stop restart
if ["$COM1" = "start"]: then
echo "ran minecraft serv"
if [ "$(pgrep -g java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar)" ] ; then
echo MineCraft Server Bukkit 1.4.7 L.T.G : Running
else
echo -e "\033[1;32m MineCraft Server Bukkit 1.4.7 L.T.G : \033[31m FAILED \033[0m"
fi
if ["$COM1" "stop"]: then
echo -e "\033[1;32m MineCraft Server Bukkit 1.4.7 L.T.G : Shutting Down \033[0m"
killall java
if (( "$(pgrep -g java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar)" )) ; then
killall java
echo -e "\033[1;32m MineCraft Server Bukkit 1.4.7 L.T.G: is SHUTDOWN \033[0m"
fi
if [ "$COM1" "Restart" ] ; then
echo MineCraft Server Bukkit 1.4.7 L.T.G : Rebooting
exit 1
fi
fi
exit 0
Let's take two lines...there's enough there to keep us busy.
read $COM1 start stop restart
if ["$COM1" = "start"]: then
The first line reads into a variable whose name is stored in $COM1 (which is uninitialized, so in fact it is empty, so it doesn't do anything after all), plus the three variables start, stop, and restart. You either need:
read COM1 start stop restart
or you need to initialize COM1 before you use it.
The second line manages to run into a surprising number of issues.
The test command, aka [, is a command name, not a symbol. As such, it needs to be separated from its arguments. Note that there usually is a command /bin/[ or /usr/bin/[, though it is also a shell built-in these days.
Fortunately, since $COM1 is unset and empty, it already is separated, but more by accident than design.
Because of that, your [ command is executed with 3 arguments: =, ]: and then. This is not a valid invocation of [. The last argument should be ] on its own.
You should use a semi-colon to separate the ] from the then.
In aggregate, you should have written:
if [ "$COM1" = "start" ]
then
or you can add a semi-colon (which does not have to be separated from the ] by a space) and then the then:
if [ "$COM1" = "start" ]; then
Stylistically, you have the string 'Minecraft Server LTG Bukkit 1.4.7' repeated all over the place; don't! Use a variable to hold it.
Worry about your embedded cursor control sequences; different terminals have different sequences, so you're restricted to a single terminal type. Fixing that is harder; investigate the tput command.
alright so this is what i ended up with- it works but i want to know if there are ways of improving such a disaster
i really do appreciate the response :)
#! /bin/bash
# script to turn the screen blue
NAME=$(echo Minecraft Server LTG Bukkit 1.4.7)
echo -e '\033[1;32m'
echo "$NAME"
echo "$NAME"
N1=$(echo LTG MineCraft Server)
echo -e '\033[31m' "[Start ] \033[1;32m $N1"
echo -e '\033[31m' "[Stop ] \033[1;32m $N1"
echo -e '\033[31m' "[Restart] \033[1;32m $N1"
echo -e '\033[0m'
echo -e "Hello \033[47m \033[30m"$USER" \033[0m Enter Command:"
echo "Command:"
read test
#this is a test piece
echo "start = $test"
if [ $test == "start" ]; then
echo -e "\033[31m $NAME : Starting"
java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar &>./minecraft.sh.rlog
if [ "$(pgrep -g java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar)" ] ; then
echo -e "\033[31m $NAME : Running"
exit 1
else
echo -e "\033[1;32m $NAME : \033[31m FAILED \033[0m"
fi
fi
if [ $test == "stop" ]; then
echo -e "\033[1;32m $NAME : Shutting Down \033[0m"
killall java &> ./minecraft.sh.log
MINECRAFT=$(pgrep -g java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar &>./minecraft.sh.log)
sleep 1
echo .
sleep 1
echo .
sleep 1
echo .
sleep 1
echo .
if [[ -z "$MINECRAFT" ]] ; then
killall java &> ./minecraft.sh.log
echo -e "\033[1;32m $NAME : IS SHUTDOWN \033[0m"
else
#should never bee seen
echo Something Went Wrong
fi
fi
if [ $test == "restart" ] ; then
echo -e "\033[31m $NAME : Rebooting \033[0m"
kill java &>minecraft.sh.log
sleep 1
echo .
sleep 1
echo .
if [[ -z "$MINECAFT" ]]; then
echo -e "\033[1;32m $NAME : IS SHUTDOWN \033[0m"
sleep 1
echo .
fi
echo -e "\033[31m $NAME : Starting \033[0m"
sleep 1
echo .
java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar &> ./minecraft.sh.rlog
else
if [ $test == "exit" ] ; then
exit 0
fi
exit 1
fi

Resources