shell script to automatically restart tomcat if stopped - linux

I am using a shell script to start tomcat server if it is not running. I am running this script in cronjob to check it frequently. This is my script
#! /bin/sh
SERVICE=/etc/init.d/tomcat7
STOPPED_MESSAGE="Tomcat Servlet Engine is not running."
if [ "`$SERVICE status`" -eq "$STOPPED_MESSAGE" ]; then
$SERVICE start
fi
But whenever I run this script, it gives me an error. If tomcat is not running then the error is :
[: ILLEGAL NUMBER : * Tomcat Servlet Engine is not running.]
And if tomcat is running the error is :
[: ILLEGAL NUMBER : * Tomcat Servlet Engine is running with pid 6130.]
I think the error is related to $SERVICE status but I am unable to resolve it. I am a new bee to shell scripting. Please help me out.
I cannot move forward until I resolve this issue.

-eq expects 2 integers for comparison (see man test). You should use = there for strings.
Or, could you be more tolerant about output from the script?
if $SERVICE status | grep -q "not running"; then
$SERVICE start
fi
Of course it would be much better to use a process monitoring tool like monit or supervisor.

Related

Function in shell script not executed correctly

I write a script to start/stop/restart a custom server application.
When starting the daemon server it should make the following:
#!/bin/sh -e
### BEGIN INIT INFO
...
...
### END INIT INFO
# Start service
pga_server_start()
{
/opt/pga/server/server -d
}
# Stop service
pga_server_stop()
{
PID=`cat /var/lock/pga_server.lock`
/bin/kill --signal SIGTERM $PID
}
pga_load_excalibur()
{
is_loaded=`lsmod | grep excalbr`
echo "Done"
if [ -z "$is_loaded" ]; then
/usr/local/bin/excload
echo "Driver excalibur loaded."
else
echo "Driver excalibur already loaded."
fi
}
case "$1" in
start)
pga_load_excalibur
pga_server_start
;;
...
...
Initialy it worked fine. Then I've added the pga_load_excalibur function.
Afterward, it does not work anymore.
It never returns from the function pga_load_excalibur.
It seems that the call to is_loaded=lsmod | grep excalbrnever returns as the subsequentecho` is never printed.
However, if I copy/paste this function in a separate shell script...it works.
But if I launch the starter script manually this way:
/etc/init.d/server start
or
service server start
it does not work.
I'm using a Debian Wheezy 7.9 x64.
Although I'm not a schell script, it looks correct. I don't understand why it does not work when it's embedded into this service starter script.
Please note that I've also tried to replace the grep line with:
is_loaded=$(lsmod | grep excalbr)
But it does not work either.
I'm running out of ideas :(
Z.
What do you get if you run the script in debug mode? try to run it with:
#!/bin/sh -xv
That may give some idea of why it's failing, post the output if you can't figure it out

SSH bash script to test if java process is running?

I need to create a SSH BASH script (on Debian linux) to test if 'java' process is running.
Here how it should look like:
IF 'java' process is not running THEN run ./start.sh
to test if java process is running, I can make this test:
ps -A | grep java
This script should run every minute (I guess in a CRON)
Regards
First of all, to run a job every minute in cron, your crontab should look like this:
* * * * * /path/to/script.sh
Next, you have a few different options for detecting a Java process.
Note that each of the following is a negation: they detect the absence of Java:
With pgrep:
if [ ! $(pgrep java) ] ; then
# no java running
fi
With pidof:
if [ ! $(pidof java) ] ; then
# no java running
fi
With ps and grep:
if [ ! $(ps -A | grep 'java') ] ; then
# no java running
fi
Of these, pgrep and pidofare probably the most efficient. Don't quote me on that, though.
The check you are doing with PS and GREP doesn't look very detailed. What if other Java processes are running ? You may detect those, and come to a wrong conclusion, because you are just checking "any" Java, not some specific Java.
With pidof would be something like this:
script.sh
pidof java;
if[$? -ne 0];
then
# here put your code when errorcode of `pidof` wasn't 0, means that it didn't find process
# for example: /home/user/start.sh
# (please don't forget to use full paths if you want to use it in cron)
fi
Especially for haters:
man pidof:
EXIT STATUS
0 At least one program was found with the requested name.
1 No program was found with the requested name.

Running Jconsole from a service: CentOS

I installed Tomcat on my CentOS 6.3 machine and I made it a service by creating the /etc/init.d/tomcat file.
It works with the basic start, stop, restart and status functionality just fine.
I use jconsole on the servers often, so I thought it would be nice to build this functionality into the service (by running service tomcat monitor) instead of having to run ps aux|grep java and then running the jconsole <Java PID> .
Here is my service script (Just the monitor section):
monitor)
# Check for Tomcat PID (greps are separated to prevent returning the single grep PID)
FOUND_PID=$(ps aux |grep $JAVA_HOME/bin/ | grep java |awk -F' ' '{print $2}')
if [[ $FOUND_PID ]]
then
echo -e $JAVA_HOME/bin/jconsole $FOUND_PID
$JAVA_HOME/bin/jconsole $FOUND_PID
else
echo -e "Failed: Tomcat is not currently running";
fi
;;
Everything inside of the monitor section works when I run the bash script directly it works, but when the service calls it, it just hangs at the jconsole line and doesn't do anything.
And when I run service tomcat monitor, I do get the correct path output, so I know that the path is correct.
Is there a way to get the jconsole to work when called from the services script?

Shell Script not running properly

I have a linux shell script that when run from command line works perfectly but when scheduled to run via crontab, it does not give desired results.
The script is quite simple, it checks to see if mysql-proxy is running or not by checking if its pid is found using the pidof command. If found to be off, it attempts to start the proxy.
# Check if mysql proxy is off
# if found off, attempt to start it
if pidof mysql-proxy
then
echo "Proxy running."
else
echo "Proxy off ... attempting to restart"
/usr/local/mysql-proxy/bin/mysql-proxy -P 172.20.10.196:3306 --daemon --proxy-backend-addresses=172.20.10.194 --proxy-backend-addresses=172.20.10.195
if pidof mysql-proxy
then
echo "Proxy started"
else
echo "Proxy restar failed"
fi
fi
echo "==============================================="
The script is saved in a file check-sql-proxy.sh and has permissions set to 777. When I run the script from command line (sh check-sql-proxy.sh) it gives the desired output.
4066
Proxy running.
===============================================
The script is also scheduled to run every 5 minutes in crontab as
*/5 * * * * bash /root/auto-restart-mysql-proxy.sh > /dev/sql-proxy-restart-log.log
However, when I see the sql-proxy-restart-log.log file it contains the output:
Proxy off ... attempting to restart
Proxy restar failed
===============================================
It seems that pidof command fails to return the pid of the running application which brings the flow of script in else condition.
I am unable to figure out how to resolve this since when I run the script manually, it works fine.
Can anyone help what I am missing with regards to permissions or settings?
Thanks in advance.
Mudasser
Check that the shell is what you think it is (usually /bin/sh, not bash)
Also check that PATH environment variable. Usually, for cron jobs it is a good practice to fully qualify all paths to binaries, e.g.
#!/bin/bash
# Check if mysql proxy is off
# if found off, attempt to start it
if /bin/pidof mysql-proxy
etc.
Try pidof /usr/local/mysql-proxy/bin/mysql-proxy (full path to executable)
In common, try use the same command name as was used to start the instance of mysql-proxy.
The problem seems that crontab environment don't have the same environment as you.
You have 2 simple & proper solutions :
In the first lines of crontab :
PATH=/foo:/bar:/qux
SHELL=/bin/bash
or
source ~/.bashrc
in your scripts.

To check status of Tomcat server in Linux

I need to know whether Tomcat 7.0.2.3 is on or not.
I got an error page that shows this message:
HTTP Status 404 - /cas/login
type Status report
message /cas/login
description The requested resource (/cas/login) is not available.
Apache Tomcat/7.0.23
I need to check the status and start the service of Tomcat in Linux. I have tried:
#/bin/startup.sh
#rctomcat7 start
But all these are not working.
Like others have pointed out, if you are receiving an HTTP standard response code of 404 then it means that Tomcat is up.
But to answer your question, you can try
#rctomcat7 status
I have used below script. This script check tomcat post and process is running up or not,if not then start the tomcat.
Tomcat Start script
if netstat -tulpen | grep 18880 && ps -ef | grep tomcat | grep java
then
echo "Tomcat Running"
else
echo "Tomcat Stopped"
/bin/sh /tomcat/bin/startup.sh
fi

Resources