Jboss 7.1.1 start/stop script - java

Could someone tell how to start/stop the Jboss-7.1.1 server in MAC using Shell Script.
stop_viewer(){
echo "********* Stopping JBoss Server by killing the process **********";
ps | grep domain.sh | grep -v grep | awk '{print $1}' | xargs kill
ps | grep java | grep -v grep | awk '{print $1}' | xargs kill
ps -ef | grep superuser | grep java | grep -v grep | awk '{print $2}'| xargs kill
echo "********* Stopped JBoss Server by killing the process **********";
}
The above script is working fine in Jboss-7.0.2 to stop the server. But in Jboss-7.1.1, it doesn't stop the server. Please someone help to solve this.

1) First you need to have JBoss downloaded. (I assume you already have valid Java version installed).
2) Once it is downloaded, unzip the folder:
cd /Users/eugene/Downloads
mkdir JBOSS-7
cp /Users/eugene/Downloads/jboss-as-7.1.1.Final.zip /Users/eugene/Downloads/JBOSS-7
cd /Users/eugene/Downloads/JBOSS-7
unzip /Users/eugene/Downloads/jboss-as-7.1.1.Final.zip
3)
cd Users/eugene/Downloads/JBOSS-7/jboss-as-7.1.1.Final/bin
./standalone.sh
If you want to stop it:
ctrl + c
of course your path may be different. If you want to run it in background, then just do:
./standalone.sh &
Stopping it :
ps -ef | grep jboss
You will get an output close to this one:
eugene#eugenes-MacBook-Pro ~/D/J/j/bin> ps -ef | grep jboss
501 1471 1446 0 1:32AM ttys000 0:03.31 /usr/....
And then issue:
kill -9 1471
Finally with JBoss CLI you can execute:
./jboss-cli.sh --connect ":shutdown"
EDIT
The Script seems to do it's job, all you have to do is edit it a bit:
#!/bin/sh
echo "********* Stopping JBoss Server by killing the process **********";
ps -e | grep jboss | grep -v grep | awk '{print $1}' | xargs kill
echo "********* Stopped JBoss Server by killing the process **********";
Notice that I removed a few lines and changed java with jboss
Put this in a file called stopJboss.sh
Then :
sudo chmod +x stopJBoss.sh
Then invoke it when needed:
./stopJBoss.sh
This will work only if you have a single instance of JBoss running, for more you will need a different script.
P.S. I am not a guru in scripting but here is what this line does:
ps -e | grep jboss | grep -v grep | awk '{print $1}' | xargs kill
It is going to look for every process that contains the jboss keyword. But it also going to output the grep command itself, thus you will get an output of two commands, but you need only the first one.
You could run ps -e | grep jboss and see that the output contains two lines and not one.
That is why you invoke grep -v grep - which means : in those two lines found grep for "grep" but invert the result, in this way you omit the second unneeded result.
Then awk '{print $1}' splits the string into tokens and takes the first one, which is the PID that you need and then you pass this PID to the kill command using the xargs command.

To shutdown the server via command line
sh ./bin/jboss-cli.sh --connect command=:shutdown
assuming you are running on localhost and using the default native management port i.e. 9999
if not you need to specify the IP (jboss.bind.address) and the native management port(jboss.management.native.port) configured in standalone.xml
sh ./bin/jboss-cli.sh --connect controller=<IP>:<native-mgmt-port> command=:shutdown

This is how I do it:
ps -ef | grep jboss | grep -v grep | awk '{print $2}' | xargs kill -9
I have this in a bash file that i call killjboss and it works well with me.

After dive on the Google, i managed to put this work:
#!/bin/sh
### BEGIN INIT INFO
# Provides: jboss
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop JBoss AS v7.1.1
### END INIT INFO
#
#source some script files in order to set and export environmental variables
#as well as add the appropriate executables to $PATH
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
export JBOSS_HOME=/home/gaspar/jboss-as-7.1.1.Final
export PATH=$JBOSS_HOME/bin:$PATH
case "$1" in
start)
echo "Starting JBoss AS 7.1.1"
#original:
#sudo -u jboss sh ${JBOSS_HOME}/bin/standalone.sh
#updated:
start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/standalone.sh
;;
stop)
echo "Stopping JBoss AS 7.1.1"
#original:
#sudo -u jboss sh ${JBOSS_HOME}/bin/jboss-admin.sh --connect command=:shutdown
#updated:
sudo -u jboss sh ${JBOSS_HOME}/bin/jboss-cli.sh --connect command=:shutdown
;;
*)
echo "Usage: /etc/init.d/jboss {start|stop}"
exit 1
;;
esac
exit 0
:)

Related

Execute bash commands via Java in Ubuntu

I have such command:
top -bn1 | grep \"Cpu(s)\" | sed \"s/.*, *\\([0-9.]*\\)%* id.*/\\1/\" | awk '{print 100 - $1}
I tried to run it with ProcessBuilder or Runtime, but I get nothing. Why?

Tomcat stops after restarting it from jenkins execution shell

I have configured jenkins to deleted tomcat existing workspace, copy the existing build to tomcat workspace and then restart tomcat. I use execute shell from jenkins. The script is the following:
#!/bin/sh
version=1.0.0-BUILD-SNAPSHOT
build_no=${BUILD_NUMBER}
if [ $build_no ]
then
cd ~
TOMCAT_WEBAPPS=`locate apache-tomcat | grep apache-tomcat | grep webapps | head -n 1 | awk '{ print $1 }'`
rm $TOMCAT_WEBAPPS/app-api.war
rm -rf $TOMCAT_WEBAPPS/app-api/*
rmdir $TOMCAT_WEBAPPS/app-api/
wget http://jenkins/job/project/ws/api/build/api-$version-$build_no-bin.zip
unzip -j connectedcare-api-$version-$build_no-bin.zip
rm api-$version-$build_no-bin.zip
cp api-$version.war $TOMCAT_WEBAPPS/app-api.war
rm api-$version.war
else
echo "Please specify the jenkins build number as an argument: "$0" <build_number>"
exit
fi
echo "Restarting tomcat ..."
TOMCAT_PID=`ps -ef | grep tomcat | grep java | awk ' { print $2 } '`
if [ $TOMCAT_PID ]
then
echo "Tomcat is running with PID" $TOMCAT_PID
echo "Forced tomcat stop with PID" $TOMCAT_PID
kill -9 $TOMCAT_PID
echo "Tomcat was stoped"
fi
echo "Starting tomcat"
TOMCAT_STARTUP_FILE=`locate apache-tomcat | grep apache-tomcat | grep startup.sh | awk ' { print $1 } '`
$TOMCAT_STARTUP_FILE
TOMCAT_PID=`ps -ef | grep tomcat | grep java | awk ' { print $2 } '`
if [ $TOMCAT_PID ]
then
echo "Tomcat is running with PID" $TOMCAT_PID
else
echo "Failed to start tomcat."
fi
When running the build, the result is as follows:
Restarting tomcat ...
Tomcat PID 10152
Tomcat is running with PID 10152
Forced tomcat stop with PID 10152
Tomcat was stoped
Starting tomcat
Tomcat started.
Tomcat is running with PID 14781
The problem is then when I am looking at the linux machine tomcat is no running, and the is nothing in the logs, so I can't figure it out what I am doing wrong. Can you please give me some suggestions?
Thank you
I managed to find a solution for this. Jenkins manipulate the environment variable called BUILD_ID. so before restarting tomcat the following line of code is needed.
export BUILD_ID=dontKillMe

How to get PID and Port # for a Jenkins Process

I need to get PID & Port# for a Jenkins process run. If i get that PID, i can kill the process if ever i need to.
I am running the Jenkins process by below commands:
java -jar jenkins.war
Sometimes, Jenkins Process fail to start if that port is taken and below occurs:
Jenkins home directory: /Users/MacPro/.jenkins found at: $user.home/.jenkins
Feb 27, 2016 10:46:09 AM org.eclipse.jetty.util.log.JavaUtilLog warn
WARNING: FAILED
SelectChannelConnector#0.0.0.0:8080:java.net.BindException:
Address already in use
java.net.BindException: Address already in use
And I know how to run the jenkins process against a specific Port#.
Need to know the commands for which PID and the port, current job is using.
The command will be below:
ps -ef| grep jenkins
It will display the process id.
This should do the job. Better to find and kill using the same command, saves time:
ps -Af | grep "jenkins" | grep -v grep | awk '{print$2}' | xargs kill -9
You can check the process before killing
ps -Af | grep "jenkins" | grep -v grep | awk '{print$2}'
If you are running Jenkins using tomcat
ps -Af | grep "tomcat" | grep -v grep | awk '{print$2}' | xargs kill -9
Please note that these commands tested on RHEL.
Answer to your question
1) In Unix box, the command usage will be ps -ef| grep jenkins, it will display the process id (pid)
2) kill -9 (pid)
You could try the following command for a cleaner output:
lsof -Pni | grep mysql

Stop Java Process if Running in Raspbian

I want to make sure that a java process in Raspbian is stopped before launching a new instance of it.
My approach, until now, has been to try to create a one-line command to stop the process from the command line, as it is the easiest way to include this step in the GO continuos delivery tool. This far, I was able to come up with this solution, which effectively stops the process:
kill $(jps -m | grep NameOfTheMainClass | awk '{print $1}')
But the problem is that when the process was not started, the kill fails as it has no pid to signal. How could I fix that?
If you prefer a one-liner
JPID=$(jps -m | grep NameOfTheMainClass | awk '{print $1}') && [ -z "$JPID" ] || kill $JPID
Try to make it conditional:
export MAIN_CLASSNAME=""
isRunning()
{
PID=`ps ax | grep MAIN_CLASSNAME | grep -v grep | awk '{print $1}'`
is_running="false"
if [ ! -z "$PID" ]; then
is_running="true"
fi
test "$is_running" = "true"
}
if isRunning; then
kill $PID
fi

How to capture the result of a system call in a shell variable?

We want to build a script that run every night (kills and restart a java process). For that we need to capture the process number (since there could be more than one java process running). The command below is basically what we will use to obtain the processes number, probably with a regexp at the end of the grep. Unless any better suggestions comes up.
root#ps -e |grep 'java'
18179 pts/0 00:00:43 java
We want to know how to parse the output above and get it into a shell variable so we can use the kill command as below.
kill -9 ${processid}
wait 10
Note1: The reason we cannot rely on the normal service stop command is because the processes sometimes does not want to die. And we have to use the kill command manually.
There are a couple of options to solve this. If you're using bash, then the shell variable '$!' will contain the PID of the last forked-off child process. So, after you start your Java program, do something like:
echo $! > /var/run/my-process.pid
Then, after your init script stops the Java process:
# Get the pidfile.
pid=$(cat /var/run/my-process.pid)
# Wait ten seconds to stop our process.
for count in $(1 2 3 4 5 6 7 8 9 10); do
sleep 1
cat "/proc/$pid/cmdline" 2>/dev/null | grep -q java
test $? -ne 0 && pid="" && break
done
# If we haven't stopped, kill the process.
if [ ! -z "$pid" ]; then
echo "Not stopping; terminating with extreme prejudice."
kill -9 $pid
fi
Make sure to remove the pidfile when you're done.
ps aux | grep java | awk '{print $1}' | xargs kill -9
Here's an explanation:
ps aux gives you a listing of all processes
grep java gives you all of the processes whose names and command line arguments contain the string "java"
awk '{print $1}' parses the output of the grep command into columns by whitespace and re-prints only the first column
xargs kill -9 passes each of the results of the awk command as parameters to a kill -9 command
I realize this is old, but what about:
pidof java | xargs kill
You can easily get the PID or list of PIDs into a variable using backticks and cut (or awk if preferred) to retrieve only the PID field:
[user#host ~]$ ps -e | grep java | cut -d' ' -f1
12812
12870
13008
13042
13060
Note in the above example I have multiple Java processes running hence the multiple values. If you save this into a variable like so:
JAVA_PROCS=`ps -e | grep java | cut -d' ' -f1`
You can iterate through the processes to kill them if desired:
for proc in $JAVA_PROCS; do
kill -9 $proc;
done
Of course, if you're only retrieving one process, then there's no need to iterate and you can just run it as:
kill -9 $JAVA_PROCS
If you do what you suggest, you may end up capturing the grep itself and killing that (since your grep command contains the java string that you are searching for). You can work around this by excluding grep (by using another grep!):
pid=`ps -e | fgrep java | fgrep -v grep | awk '{print $1}'`
# check pid has a value
# kill $pid
You might also like ps -e -opid,args.
A better alternative is to use pgrep(1) or pkill(1) if your system has them. No more pipes, seds, awks, cuts, xargs:
pkill -9 java
I use something like this:
kill $(ps -A | grep java | cut -b 1-5)
killing it:
ps -e | grep java | cut -f1 -d' ' | xargs kill -9
storing PID on variable:
export JAVAPID=`ps -e | grep 'java' | cut -f1 -d' '`
checking that it worked:
echo $JAVAPID

Categories

Resources