0

We are trying to run a script to stop elasticsearch.

When I execute ps aux |grep elastic I am getting:

root@baldelas01:/tmp# ps aux |grep elastic
root     3016819  0.0  0.1   8248  4164 pts/2    S    14:43   0:00 su elasticsearch
elastic+ 3016820  0.0  0.1   7460  4260 pts/2    S+   14:43   0:00 bash
elastic+ 3016870  0.8 33.9 5562772 1369948 pts/2 Sl   14:43   0:37 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.io.tmpdir=/tmp/elasticsearch-3054796781310172182 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=32 -XX:GCLogFileSize=64m -Des.path.home=/opt/elasticsearch-6.6.1 -Des.path.conf=/opt/elasticsearch-6.6.1/config -Des.distribution.flavor=default -Des.distribution.type=tar -cp /opt/elasticsearch-6.6.1/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
elastic+ 3016887  0.0  0.1 121920  6900 pts/2    Sl   14:43   0:00 /opt/elasticsearch-6.6.1/modules/x-pack-ml/platform/linux-x86_64/bin/controller
root     3019325  0.0  0.0   6432   736 pts/1    R+   15:54   0:00 grep --color=auto elastic
root@baldelas01:/tmp#

How do I get the PID : 3016870 in the script and execute kill -9?

I tried pidof but that does not give any result.

muru
  • 197,895
  • 55
  • 485
  • 740
  • same as https://askubuntu.com/questions/1176341/find-process-id-by-command-used-by-the-process-and-kill-it replace ffmpeg by elasticsearch and https://askubuntu.com/questions/80211/process-id-using-ps-aux or https://askubuntu.com/questions/490961/getting-pid-of-program-via-terminal and https://askubuntu.com/questions/7203/whats-the-terminal-command-for-finding-out-a-processs-id – Rinzwind Mar 29 '21 at 11:06
  • Why not sudo service elasticsearch stop ? See also https://www.elastic.co/guide/en/elasticsearch/reference/6.5/stopping-elasticsearch.html They have a specific section on how to stop elasticsearch and why you should use specific commands. Killing should be the last resort ;) They also include a version using a pid :) – Rinzwind Mar 29 '21 at 11:16
  • Hi Rinzwind, I tried the steps in the link https://www.elastic.co/guide/en/elasticsearch/reference/6.5/stopping-elasticsearch.html when i execute jps | grep Elasticsearch i get a result : 3016870 Elasticsearch. We have elastic search in /opt/elasticsearch-6.6.1 , I then navigated to that location and executed ./bin/elasticsearch -p /tmp/elasticsearch-pid -d however i dont see any file created /tmp by name elasticsearch-pid instead i see the file as elasticsearch-7267875706287918534 and inside that jna-3506402 . Hence, cat /tmp/elasticsearch-pid && echo does not result any PID. Please Help – Aravind Viswanathan Mar 30 '21 at 08:24

1 Answers1

0

the pidof doesn't give any result because elastcisearch is run through java binary.

the not-so-simple solution to get the id would be:

ps -aux | grep 'elastic' | grep 'java' | awk -F " " '{ print $2 }'

which is:

  • print all processes ( ps -aux )
  • filter the result by 'elastic' keyword ( grep 'elastic' )
  • filter the result by 'java' keyword ( grep 'java' )
  • only print second column ( awk -F " " '{ print $2 }' )

then you can kill specific ID using kill -9 if you really need to. However, please note it's always better to securely shut down the service by using the command suggested by Rinzwind in one of the comments.

Jan Myszkier
  • 1,243
  • Thanks Jan, When i executed ps -aux | grep 'elastic' | grep 'java' | awk -F " " '{ print $2 }' i dont get any result. root@baldelas01:/opt# ps -aux | grep 'elastic' | grep 'java' | awk -F " " '{ print $2 }' root@baldelas01:/opt# . So i tried removing "-" and used ps aux | grep 'elastic' | grep 'java' | awk -F " " '{ print $2 }' got the result. Thanks let me try this in script and see if it works. – Aravind Viswanathan Mar 30 '21 at 08:25
  • In the Script i tried stop) echo "Stopping $APP" processID="ps -ef | grep 'elastic' | grep 'java' | awk -F " " '{ print $2 }'" echo $processID sleep 30 kill $processID #/bin/su -m $USER -c "kill -9 (ps -ef | grep elastic) &> /dev/null" sleep 30 echo "$APP stopped successfully" However when i execute the restart file i get Stopping ElasticSearch ./elastic-auto-restart: 22: '{ print }': not found ;; – Aravind Viswanathan Mar 30 '21 at 08:37
  • Please ask that as a separate question because: pasting script in a comment makes in barely readable. you're telling us there's issue with line 22, but you're not pasting the full script. I can only see ~7 lines of it. then again I think you're still trying to kill something manually which already has a stop script in /etc/init.d/elasticsearch (hint: yes, it also contains a location of elasticsearch PID file, which contains process ID) – Jan Myszkier Mar 30 '21 at 09:59
  • Thanks i have created a seperate question as per the request with full script. https://askubuntu.com/questions/1334513/related-to-how-do-i-kill-elasticsearch-from-the-command-line – Aravind Viswanathan Apr 28 '21 at 08:10
  • Hi All, Please help – Aravind Viswanathan May 10 '21 at 05:58