I am trying to create a script that monitors a java runtime process,which makes use of a Virgo service. It should start the process when its not running. As there is no specific service name, i was thinking of grepping the name of the process and use it as an identifier.
Here is the code i thought of:
#!/bin/sh
service=virgo
ps auxw | grep $service | grep -v grep > /dev/null
if [ $? != 0 ]
then
/opt/apromore/ApromoreCode/build.xml start-virgo > /dev/null
else
echo Apromore is running already
fi
and this is the process I try to monitor (value from ps aux)
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java -classpath usr/share/ant/lib/ant-launcher.jar:/ usr/share/java/xmlParserAPIs.jar:/usr/share/java/xercesImpl.jar -Dant.home=/usr/ share/ant -Dant.library.dir=/usr/share/ant/lib org.apache.tools.ant.launch.Launc her -cp start-virgo
but even when the process is running, i cant identify it with the mentioned script. There is a error called $'\r'. I have no idea how to get rid of it.
Related
I am working on a Gradle Java project. Which starts Tomcat for testing and stops it later.
I am supposed to kill this Tomcat instance when the test fails.
I tried using "ps aux| grep tomcat | grep -v grap | awk {print $2}" command to get the process id and kill the process.
But on Production, there will be so many Tomcat processes running simultaneously by many users, I just want the tomcat process started by my build.gradle for test execution.
So how can I accomplish the task? Please provide me some guidelines.
You need to find a unique string in the output of 'ps aux' which differentiates your test tomcat and others'.
I currently use the below script to run 'shutdown.sh' first and then kill the PID as most of the times, the application stops but the process does not stop.
PID=`ps -ef | grep $JAVA_HOME/bin/java | grep "$TOMCAT_LOC"/conf | grep -v grep | awk '{ print $2 }'`
if [ $PID ]; then
echo tomcat is running with PID:$PID.
# Stop or Kill running Tomcat
if [[ -f $TOMCAT_LOC/bin/shutdown.sh ]]; then
[[ ! -x $TOMCAT_LOC/bin/shutdown.sh ]] && chmod a+x $TOMCAT_LOC/bin/shutdown.sh
$TOMCAT_LOC/bin/shutdown.sh >>/dev/null
sleep 20
fi
kill -9 $PID
sleep 3
else
echo tomcat is not running
fi
You may also look at configuring a PID file by editing the 'catalina.sh' which you can read later to find out your PID.
# CATALINA_PID (Optional) Path of the file which should contains the pid
# of the catalina startup java process, when start (fork) is
# used
Java JRE has tool called jps in $JAVA_HOME/bin folder.
It's similar to unix ps command but for java only.
You can use it to determined exac java process you need.
Using this tool is more recommended and actually it is more useful, when you have more than one java applications is running on your host...
for example I have running h2 database and many other apps, but wanna kill only h2, so I can use jps to get it PID
$ jps
17810 GradleDaemon
17798 GradleWrapperMain
17816 h2-1.4.197.jar
17817 GradleDaemon
17818 GradleDaemon
18011 Jps
16479
and then just kill needed process:
kill -9 17816
and all other java apps will continue work normally. I not sure about tomcat, but I think it can be done in similar way, something like that:
kill -9 $(jps | grep tomcat | awk '{print $1}')
Lastly, little bit offtopic, but a specially to your case: correct way would be using start/stop/restart scripts provided by tomcat
The correct way to terminate a Tomcat instance is via its own shutdown command. You should not be thinking of processes, or PIDs, or kills, at all.
so if you want to kill tomcat from that user from which you have logged in then try following and let me know if this helps you.
ps -ef | grep -v grep | grep `whoami` | grep tomcat
So by ps -ef I am listing all the processes then grep -v grep will remove the grep command's process then grep whoami will look for your currently logged in user then grep tomcat will look only for tomcat process, test it once and if All is Well then you could kill it.
By the way how about tomcat stop script? In case it is there you could use that also.
You can use shell variable $!. It represents the PID of the most recent background command.
yourCommand &
CMD_PID=$!
echo $CMD_PID
Im a new to docker and i am trying to create a container running multiple services,
using this documentation:Run multiple services in a container
I've managed to get Java and Nodejs installed on the containe, eventually leading to running this script at the end of the Dockerfile as an ENTRYPOINT:
#!/bin/bash
# Start the first process
/tmp/cliffer/bin/startup.sh &
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start my_first_process: $status"
exit $status
fi
# Start the second process
npm start &
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start my_second_process: $status"
exit $status
fi
# Naive check runs checks once a minute to see if either of the processes exited.
# This illustrates part of the heavy lifting you need to do if you want to run
# more than one service in a container. The container will exit with an error
# if it detects that either of the processes has exited.
# Otherwise it will loop forever, waking up every 60 seconds
while /bin/true; do
PROCESS_1_STATUS=$(ps aux |grep -q my_first_process |grep -v grep)
PROCESS_2_STATUS=$(ps aux |grep -q my_second_process | grep -v grep)
# If the greps above find anything, they will exit with 0 status
# If they are not both 0, then something is wrong
if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit -1
fi
sleep 60
done
both services are running in the background, and the result is that the npm start, starts a webserver but immediately shuts it down.
This is the output im getting from the npm start &
[--:--:--][CONSOLE] [09:19:11] [Start] Listening at port 3000
[09:19:11] [Stop] Shutting down
when I run the the container with each of the services separately in a container of its own it works perfect.
any ideas why?
Docker needs one process to run in the foreground otherwise it will exit. Your foreground program is your entrypoint that sleeps for 60 seconds and checks if both processes are still in the background.
The check is for the process name "my_first_process". That should be for your instance be something like "java"
so instead of
ps aux |grep -q my_first_process |grep -v grep
ps aux |grep -q my_second_process |grep -v grep
try
ps aux |grep -q java |grep -v grep
ps aux |grep -q npm |grep -v grep
Joel's comment is still valid, it makes more sense to run two different docker containers for your use case.
I'm trying to do it like this:
file_name=$1
#Clear log file
echo > nohup.out
for PID in $(ps aux | grep service_name | awk {'print $2'}); do
kill -9 $PID;
done
echo "Killed old process"
nohup java -Xms256m -Xmx256m -XX:MaxMetaspaceSize=256m -jar $file_name &
echo "Started new process"
But it's just printing "Killed" and doesn't start it again.
If I do all this stuff separately - it's working, but for all together - not...
Допоможіть хто зможе...
The script from which you execute it, does it contain "service_name" in its name? Then it obviously fulfills the criteria for begin killed by your for loop and commits suicide.
If it does not, then still the grep service_name remains which fulfills the kill criteria (has service_name in its cmd line) - for fixed strings in grep I usually write them grep "s[e]rvice_name", other prefer to put another |grep -v grep into the pipe before the awk
I have a java app on my (Ubuntu) server. If I do this, then it starts correctly:
/usr/bin/java -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main
but I don't know how to get its PID. I don't know how to stop it with an init.d script.
I have a different app, written in Clojure, and for it I was able to write an init.d script that works great. So I tried to refashion that init.d script for my Java app, and this is what I got:
WORK_DIR="/home/jenkins/veta"
NAME="lily"
JAR="lily.jar"
USER="jenkins"
DAEMON="/usr/bin/java"
DAEMON_ARGS=" -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main"
start () {
echo "Starting lily..."
if [ ! -f $WORK_DIR/lily.pid ]; then
/sbin/start-stop-daemon --start --verbose --background --chdir $WORK_DIR --exec $DAEMON --pidfile $WORK_DIR/lily.pid --chuid "$USER" --make-pidfile -- $DAEMON_ARGS
else
echo "lily is already running..."
fi
}
stop () {
echo "Stopping lily..."
/sbin/start-stop-daemon --stop --exec $DAEMON --pidfile $WORK_DIR/lily.pid
rm $WORK_DIR/lily.pid
}
But this doesn't work. Although the PID in $WORK_DIR/lily.pid changes every time I run the script, no process with that PID ever seems to run. If I try:
ps aux | grep java
I don't see this app, nor if I try using the PID.
So is there a way I can use the first command, but somehow capture the PID, so I can store it for later?
I just want a reliable way to stop and start this app. That can be by PID or some other factor. I'm open to suggestions.
UPDATE:
Maybe my question is unclear? Something like jps or ps will give me too many answers. If I do something like "ps aux | grep java" I'll see that there are 5 different java apps running on the server. The start-stop-daemon won't know which PID belongs to this particular app, nor can I figure out what I should feed into my init.d script.
If your system has jdk installed there is an utility called jps which resides in jdk/bin. It will display the list of running java process. Make use of it.
If jdk is not installed in your machine then you have to grep the java process from ps -eaf command.
If you want the pid from the command line, this might work:
myCommand & echo $!
Which I copied from the accepted response to a very similar topic in ServerFault: https://serverfault.com/a/205504
I have a java jar program that I am trying to run on startup of my machine. Ideally, the shells script will check every 60 seconds to assure that the jar is running. How do I check if the jar is running on centos, this does not appear to be working?
My current .sh file:
#!/bin/bash
while [ true ]
do
cnt=`ps -eaflc --sort stime | grep /home/Portal.jar |grep -v grep | wc -l`
if(test $cnt -eq 3);
then
echo "Service already running..."
else
echo "Starting Service"
java -jar /home/Portal.jar >> /dev/null &
fi
sleep 1m
done
I used this for referencing so far.
Depending on what your program does, there may be more or less intelligent ways to check it. For example, if you have some server, it will listen on a port.
Then something like
netstat -an | fgrep tcp | fgrep LISTEN | fgrep :87654 # or whatever your port is
could do the job.
Then there is lsof, which could also detect listening ports.
Finally, you could connect and issue a pseudo request. For example, for a http server, you could use lynx or curl. For a server with a non-stamdard protocol, you can write a small client program whose sole purpose is to connect to the server just to see if it is there.
Store your process id in file and check for this process.
#!/bin/bash
while [ true ]
do
pid=$(cat /tmp/portal.pid)
if [[ -n "$pid" && $(ps -p $pid | wc -l) -eq 2 ]]
then
echo "Service already running..."
else
echo "Starting Service"
java -jar /home/Portal.jar >> /dev/null &
echo $! > /tmp/portal.pid
fi
sleep 1m
done
/tmp will be cleared on restart, all right in this case.
I did the very same scenario a couple of months ago. My task was to ensure a jar distributed java program to run 24/7 on a Linux server.
My program was console-based, started, did something then stopped.
I did a shell script that started, waited to end and then re-started the app in an infinite loop.
I installed runit, created a service and supplied this script as the run script. Works very well.
In general, the shell script ensures that the java program is running and runit ensures that the start script (which is our script) is running.
You find valuable info here: http://smarden.org/runit/faq.html
Rather than putting the process to sleep , I'd rather have it exit and use crontab to run the process every 1 min;which will check if its running or else just stop the script.
#!/bin/sh
declare -a devId=( "/Path/To/TestJar.jar Test1" "/Path/To/TestJar.jar Test2" ) #jarfile with pathname and Test as argument
# get length of an array
arraylength=${#devId[#]}
# use for loop to read all values and indexes
for (( i=1; i<${arraylength}+1; i++ ));
do
y=${devId[$i-1]}
cnt=`ps -eaflc --sort stime | grep "$y" |grep -v grep | wc -l`
if [ $cnt = 0 ]
then
java -jar $y& > /dev/null
b=$(basename $y)
echo $b
#DO SOME OPERATION LIKE SEND AN EMAIL OR ADD TO LOG FILE
continue
elif [ $cnt != 0 ]
then
echo 'do nothing'
fi
done
Why do you think $cnt should be equal to 3? Shouldn't it be equal to 1 if the process is already running?
You could use the jps command. It return the JVMs running in the system.
I created following script to monitor my application jar is running or not.
In this case My application jar is running on port 8080
#!/bin/bash
check=$(netstat -an | grep 8080 | wc -l)
if [[ $check -eq 0 ]];then
echo "jar is not running..."
/usr/bin/java -jar /path/to/target/application.jar >> /dev/null &
else
echo "it is running"
fi
I am using cronjob to monitor jar app by executing shell script on every minute.
$ crontab -e
in the end of file
* * * * * /bin/bash monitor-jar.sh /dev/null 2>&1