I want to check the status of an application if it's running or not.
For that I have written a below script however being a newbie I am not sure how can I use condition here such that if "process(pid) is running and URL is accessible then only Application is running OK".
code:
#!/bin/bash
#Check: for java connector service
echo "Checking health of java connectors"
PID=$(ps -ef | grep test-core | grep -v grep | awk '{print $2}')
if [ -n "$PID" ]; then
echo "INFO: Java connector service is running"
else
echo "WARNING: Java connector service is not running"
fi
#Check for UI status
status="$(curl -Is http://172.217.23.16/application/authenticate | head -1)"
validate=( $status )
if [ ${validate[-2]} == "200" ]; then
echo "INFO: Application URL is up and running"
else
echo "WARNING: Application URL is NOT RESPONDING"
fi
I am looking for something like if cond1 && cond2 satisfies then "Application is running OK".
A guidance here would be really helpful. Thanks!
Since you have already calculated the values of PID and validate, you can combine the tests of your current code into a single one, by doing
if [[ -n $PID && ${validate[-2]} == 200 ]]
then
...
fi
Related
I want to monitor apache service for ubuntu but below script is not working.
#!/bin/bash
if [ $(ps aux | grep apache2 | wc -l) -gt 1 ]; then
echo "Statistic:0"
else
echo "Statistic:1"
fi
exit 0
I edited your question to make it readable - but I think it is working as expected once the format is cleaned up. It looks like you are trying to find out if apache2 is running.
#!/bin/bash
if [ $(ps aux | grep apache2 | wc -l) -gt 1 ]; then
echo "Statistic:0"
else
echo "Statistic:1"
fi
exit 0
Depending on what "flavor" of linux you are running, the following might be a better option for you. You can also use is-enabled, is-failed, to check other status.
if systemctl is-active -q apache2; then
echo "Statistic:0"
else
echo "Statistic:1"
fi
I have to detect if elastic search is running on Linux. If it is not running than start it machine using shell script.
Used following code to detect if elastic search is running, but every time else condition is executed even if service is running:
service=elasticsearch
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running!!!"
else
echo "$service is not running!!!"
fi
Here is a sample script and used in a shell script to check if my sevice is running:
Take a look at this script to see if it helps you.
#verify if the service is running
ps aux | grep SERVICENAME | pidof java > /dev/null
verifier=$?
zero=0
if [ $zero = $verifier ]
then
echo "Service was successfully started"
else
echo "Service stopped"
fi
In my case my service was done in java then I use the pidof to properly filter my service
The issue with checking for pid is that the service might have started, but it might not be ready actually. So instead, I check for port that ES is listening on. It works better for me.
netstat -uplnt | grep :9200 | grep LISTEN > /dev/null
verifier=$?
if [ 0 = $verifier ]
then
echo "ES is listening on port 9200"
else
echo "ES is not ready yet"
fi
Is there some linux/jboss command I can use in a script to see if jboss started.
I have to start up a couple in a specified order and one of the jbosses must
be started before the others can be started up.
/T
Refer this link ,With the following command you can try to read the server "Started" attribute
twiddle get "jboss.system:type=Server" Started
Started=true
On Fedora 19, you can install the jboss-as package, which comes with a nice startup script, and you can check the status just like any other daemon: systemctl status jboss-as
Similarly for Fedora 20 and Wildfly: systemctl status wildfly .
Here is a script I used:
#!/bin/bash
CHECK_TIMEOUT=$1;
if [[ $1 =~ ^[0-9]+$ ]]; then
echo "Checking if JBoss is running with timeout of $1 s.";
else
echo "Checking if JBoss is running with default timeout of 60 s.";
CHECK_TIMEOUT=60;
fi
while [[ $CHECK_TIMEOUT -ne 0 ]]
do
sleep 1;
JBOSS_STATE=`~/jboss/bin/jboss-cli.sh 'connect,:read-attribute(name=server-state),q' | grep result`;
if [[ -z $JBOSS_STATE ]]; then
JBOSS_STATE="stopped";
else
JBOSS_STATE=`echo "$JBOSS_STATE" | tr -s \" " " | cut -d ' ' -f 4`;
fi
echo "JBoss is $JBOSS_STATE";
if [[ $JBOSS_STATE == "running" ]]; then
exit 0;
fi
((CHECK_TIMEOUT-=1));
done
exit 1;
I'm made the following code to determine if a process is running:
#!/bin/bash
ps cax | grep 'Nginx' > /dev/null
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running."
fi
I would like to use my code to check multiple processes and use a list as input (see below), but getting stuck in the foreach loop.
CHECK_PROCESS=nginx, mysql, etc
What is the correct way to use a foreach loop to check multiple processes?
If your system has pgrep installed, you'd better use it instead of the greping of the output of ps.
Regarding you're question, how to loop through a list of processes, you'd better use an array. A working example might be something along these lines:
(Remark: avoid capitalized variables, this is an awfully bad bash practice):
#!/bin/bash
# Define an array of processes to be checked.
# If properly quoted, these may contain spaces
check_process=( "nginx" "mysql" "etc" )
for p in "${check_process[#]}"; do
if pgrep "$p" > /dev/null; then
echo "Process \`$p' is running"
else
echo "Process \`$p' is not running"
fi
done
Cheers!
Use a separated list of of processes:
#!/bin/bash
PROC="nginx mysql ..."
for p in $PROC
do
ps cax | grep $p > /dev/null
if [ $? -eq 0 ]; then
echo "Process $p is running."
else
echo "Process $p is not running."
fi
done
If you simply want to see if either one of them is running, then you don't need loo. Just give the list to grep:
ps cax | grep -E "Nginx|mysql|etc" > /dev/null
Create file chkproc.sh
#!/bin/bash
for name in $#; do
echo -n "$name: "
pgrep $name > /dev/null && echo "running" || echo "not running"
done
And then run:
$ ./chkproc.sh nginx mysql etc
nginx: not running
mysql: running
etc: not running
Unless you have some old or "weird" system you should have pgrep available.
I have a script below that I'd like to echo out "jboss not running" or "jboss is running" depending on whether it can find the jboss process in the process list. However, when I shut down Jboss it still executes the Else condition and says "jboss is running". If I manually do "pgrep -f jboss" it doesn't return anything, so why is it still going into the Else condition? puzzled
#!/bin/bash
if [ -z "$(pgrep -f jboss)" ]
then
echo "jboss is not running"
else
echo "jboss is running"
fi
Thanks for your help!
Instead of checking the output, just use the command:
if pgrep -f jboss >/dev/null
then
echo "jboss is running"
else
echo "jboss is not running"
fi
Get the process ID JBoss 7 / EAP 6:
pgrep -f org.jboss.as
So if you want to enhance the former example script with this:
if [ -z "$(pgrep -f org.jboss.as)" ]
then
echo "JBoss is NOT running"
else
echo "JBoss is running"
fi
Try using the exit status of the command -
#!/bin/bash
pgrep -f jboss &> /dev/null
if [ $? -eq 0 ]
then
echo "jboss is running"
else
echo "jboss is not running"
fi
This is the best way:
if [ -z "$(ps -ef | grep java | grep jboss)" ]
then
echo "JBoss is NOT running"
else
echo "JBoss is running"
fi
#! /bin/bash
if [ -z "$(ps -ef | grep org.jboss.Main)" ]
then
echo "jboss is not running"
else
echo "jboss is running"
fi
The best way is use this
result=`$jboss/bin/jboss-cli.sh --connect controller=localhost:$controller_port --commands=\"read-attribute server-state\" > out 2&1`
echo "$result" | grep -q "running"
if [ $? -eq 0 ];then
echo "Jboss running"
fi
If you want to check if ear or war file is deployed then you could use the following command
$JBOSS_HOME/bin/jboss\-cli.sh --connect controller=localhost:$PORT --command="deployment-info --name=$YOUR_WAR_OR_EAR_FILE"
Run jps, grep it for the line with you jboss, cut the PID from the line and check the PID.
you can use below function in bash script; below function return PID of Jboss if Jboss is started yet. notice that $1 is base directory of your jboss. for example:
$1=/root/app/wildfly-X.Final
function getJbossPID() {
local pid;
local DIR=$1;
if [[ ! -z $DIR ]]; then
local result=();
jps=`jps -m`
if [[ ! -z $jps ]]; then
IFS='
'
jps_result=`jps -m`
for jps_line_result in $jps_result; do
result_jps=$jps_line_result;
if [[ $result_jps == *$DIR* ]]; then
unset IFS
array=( $result_jps );
pid=${array[0]};
fi
IFS='
'
done;
unset IFS;
fi
fi
echo $pid;
}
you can use above function same as below:
PID=$(getJbossPID $YOUR_JBOSS)
You cant check the jboss in that way.
Because Jboss started sometimes log time like 1-2 minutes.
So your script shows only process Jboss, not the "Jboss is really UP" (I mean application on JBoss).