0

I am writing a bash script to check if certain application is running or not. If its dead i will simply restart it.

My initial though was, check if there is one pid for that process(which in this case would be a Java Thread).

ps aux | grep org.apache.flume.node.Application
root     15881  5.1  1.1 3286884 93996 pts/3   Sl   00:25   0:26 /usr/lib/jvm/java-1.8.0-ibm-1.8.0.3.20-1jpp.1.el7_2.x86_64/jre/bin/java -Xms100m -Xmx500m -Dcom.sun.management.jmxremote? -Dflume.monitoring.type=http -Dflume.monitoring.port=34545 -cp /prayagupd/opt/flume/conf:/prayagupd/opt/flume/lib/*:/lib/* -Djava.library.path= org.apache.flume.node.Application -f /prayagupd/opt/flume/conf/flume.conf -n client
root     18716  0.0  0.0 112780   976 pts/3    R+   00:34   0:00 grep --color=auto org.apache.flume.node.Application

Based on my logic, I would check if there's one process id for given name but ps aux also counts the command that I run to grep the processes.

ps aux | grep org.apache.flume.node.Application | awk '{print $2}'
15881
22878

I can do a check

if(I find 2 processIds) then 
      count it as running process(as shown in below command)
otherwise 
      re-start it again.

eg.

ps aux | grep org.apache.flume.node.Application | awk '{print '$2'}' | wc -l
2

But, I feel there's a better way to check to if some process is running.

prayagupa
  • 4,897

1 Answers1

0

This will give the count

ps aux | grep -c "[o]rg.apache.flume.node.Application"
Kamaraj
  • 4,365