How to check if JBoss is running on Unix server? - linux

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).

Related

shell script to use condition

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

Linux Script to check if process is running and restart if not

I am having this script which looks for the process filebeat and restarts it if is not running. Cron runs this script every 5 minutes. Most of the time this works fine except sometime it creates multiple filebeat process. Can someone please point out what is the issue in my script.
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
service=filebeat
servicex=/usr/share/filebeat/bin/filebeat
pid=`pgrep -x "filebeat"`
if [ $pid > /dev/null ]
then
echo "$(date) $service is running!!!"
else
echo "$(date) starting $service"
cd /home/hpov/beats/filebeat
./filebeat -c filebeat.yml &
fi
#!/bin/bash
pidof script.x86 >/dev/null
if [[ $? -ne 0 ]] ; then
echo "Restarting script: $(date)" >> /var/log/script.txt
/etc/script/script.x86 &
fi
Super easy :D

How to see JBOSS has started/stopped from commandline

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;

Bash script to check multiple running processes

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.

check if file exists on remote host with ssh

I would like to check if a certain file exists on the remote host.
I tried this:
$ if [ ssh user#localhost -p 19999 -e /home/user/Dropbox/path/Research_and_Development/Puffer_and_Traps/Repeaters_Network/UBC_LOGS/log1349544129.tar.bz2 ] then echo "okidoke"; else "not okay!" fi
-sh: syntax error: unexpected "else" (expecting "then")
In addition to the answers above, there's the shorthand way to do it:
ssh -q $HOST [[ -f $FILE_PATH ]] && echo "File exists" || echo "File does not exist";
-q is quiet mode, it will suppress warnings and messages.
As #Mat mentioned, one advantage of testing like this is that you can easily swap out the -f for any test operator you like: -nt, -d, -s etc...
Test Operators: http://tldp.org/LDP/abs/html/fto.html
Here is a simple approach:
#!/bin/bash
USE_IP='-o StrictHostKeyChecking=no username#192.168.1.2'
FILE_NAME=/home/user/file.txt
SSH_PASS='sshpass -p password-for-remote-machine'
if $SSH_PASS ssh $USE_IP stat $FILE_NAME \> /dev/null 2\>\&1
then
echo "File exists"
else
echo "File does not exist"
fi
You need to install sshpass on your machine to work it.
Can't get much simpler than this :)
ssh host "test -e /path/to/file"
if [ $? -eq 0 ]; then
# your file exists
fi
As suggested by dimo414, this can be collapsed to:
if ssh host "test -e /path/to/file"; then
# your file exists
fi
one line, proper quoting
ssh remote_host test -f "/path/to/file" && echo found || echo not found
You're missing ;s. The general syntax if you put it all in one line would be:
if thing ; then ... ; else ... ; fi
The thing can be pretty much anything that returns an exit code. The then branch is taken if that thing returns 0, the else branch otherwise.
[ isn't syntax, it's the test program (check out ls /bin/[, it actually exists, man test for the docs – although can also have a built-in version with different/additional features.) which is used to test various common conditions on files and variables. (Note that [[ on the other hand is syntax and is handled by your shell, if it supports it).
For your case, you don't want to use test directly, you want to test something on the remote host. So try something like:
if ssh user#host test -e "$file" ; then ... ; else ... ; fi
Test if a file exists:
HOST="example.com"
FILE="/path/to/file"
if ssh $HOST "test -e $FILE"; then
echo "File exists."
else
echo "File does not exist."
fi
And the opposite, test if a file does not exist:
HOST="example.com"
FILE="/path/to/file"
if ! ssh $HOST "test -e $FILE"; then
echo "File does not exist."
else
echo "File exists."
fi
ssh -q $HOST [[ -f $FILE_PATH ]] && echo "File exists"
The above will run the echo command on the machine you're running the ssh command from. To get the remote server to run the command:
ssh -q $HOST "[[ ! -f $FILE_PATH ]] && touch $FILE_PATH"
Silent check if file exist and perform if not
if ! ssh $USER#$HOST "test -e file.txt" 2> /dev/null; then
echo "File not exist"
fi
You can specify the shell to be used by the remote host locally.
echo 'echo "Bash version: ${BASH_VERSION}"' | ssh -q localhost bash
And be careful to (single-)quote the variables you wish to be expanded by the remote host; otherwise variable expansion will be done by your local shell!
# example for local / remote variable expansion
{
echo "[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'" |
ssh -q localhost bash
echo '[[ $- == *i* ]] && echo "Interactive" || echo "Not interactive"' |
ssh -q localhost bash
}
So, to check if a certain file exists on the remote host you can do the following:
host='localhost' # localhost as test case
file='~/.bash_history'
if `echo 'test -f '"${file}"' && exit 0 || exit 1' | ssh -q "${host}" sh`; then
#if `echo '[[ -f '"${file}"' ]] && exit 0 || exit 1' | ssh -q "${host}" bash`; then
echo exists
else
echo does not exist
fi
I wanted also to check if a remote file exist but with RSH. I have tried the previous solutions but they didn't work with RSH.
Finally, I did I short function which works fine:
function existRemoteFile ()
{
REMOTE=$1
FILE=$2
RESULT=$(rsh -l user $REMOTE "test -e $FILE && echo \"0\" || echo \"1\"")
if [ $RESULT -eq 0 ]
then
return 0
else
return 1
fi
}
On CentOS machine, the oneliner bash that worked for me was:
if ssh <servername> "stat <filename> > /dev/null 2>&1"; then echo "file exists"; else echo "file doesnt exits"; fi
It needed I/O redirection (as the top answer) as well as quotes around the command to be run on remote.
This also works :
if ssh user#ip "[ -s /path/file_name ]" ;then
status=RECEIVED ;
else
status=MISSING ;
fi
#its simple
if [[ "`ssh -q user#hostname ls /dir/filename.abc 2>dev/null`" == "/dir/filename.abc" ]]
then
echo "file exists"
else
echo "file not exists"
fi

Resources