-1

In my current case, i have one jar file which is running on server. When i need to deploy it again, then i need to stop the jar file and run the new jar file using shell script

by doing java -jar xyz.war & it executes the jar file. But to check the pid i need to check again by using command pgrep -f xyz.jar it gives the process id. Now how do i store the process id so that i can use it while killing and then run the new jar when deployment is going.

#!/bin/sh
if pgrep -x xyz.jar > /dev/null
then
    echo "Running"
else
    echo "Stopped"
    cd /folder/location
    java -jar xyz.jar &
fi

can anyone help me how do i get the process id in the if condition part

Prateek Naik
  • 1,952
  • 3
  • 15
  • 32
  • Editing your question to incorporate suggestions from answers is not really good form. You clearly introduced some new errors instead of the old ones, and the answer you got is now meaningless in the context of your essentially new question. Please roll back your edit, or explain how this is still the same question. – tripleee Feb 15 '18 at 11:33
  • `if command1; command2; then ...` will basically ignore the result from `command1` though it may of course have useful side effects; but that is clearly not what's going on here. – tripleee Feb 15 '18 at 11:34
  • Are you really sure `kill -9` is required here? See also [useless use of `kill -9`](http://www.iki.fi/era/unix/award.html#kill) – tripleee Feb 15 '18 at 11:34
  • yes i need to kill the process right. – Prateek Naik Feb 15 '18 at 11:42
  • That's what `kill` does. In the vast majority of cases, signal number 9 is *not* what you should use. – tripleee Feb 15 '18 at 11:45

2 Answers2

1

just store pid in separate variable:

apid=$(pgrep -x xyz.war)

then use it as $apid

apid=$(pgrep -x xyz.war) 
if [ -n "$apid" ]; then 
    .............. 
fi
Lemurata
  • 215
  • 2
  • 12
1

Here's how you would use the answer you already received.

#!/bin/sh
if apid=$(pgrep -f xyz.jar)
then
    echo "Running, pid is $apid"
else
    echo "Stopped"
    cd /folder/location
    java -jar xyz.jar &
fi

You should not kill -9 the process, because that prevents Java from doing any cleanups before terminating -- you should regularly use just kill unless you know there are bugs in the code which prevent this from working (and even then probably try a regular kill before bringing out the heavy artillery with kill -9).

It's not clear from your question in which scenario you would kill it (do you want to kill it when you find that it's already running?) In any event, this is a very common FAQ -- please look for duplicates e.g. in the list of common questions in the Stack Overflow bash tag info page

tripleee
  • 158,107
  • 27
  • 234
  • 292
  • its not printing any `apid` value for this `echo "Running, pid is $apid"` line Actually its not going inside the `if` condition, directly going inside `else` and printing `echo "Stopped"`, when the `jar` is running – Prateek Naik Feb 15 '18 at 12:59
  • Then your `pgrep -x` is different from mine. Can you verify by other means that the process you are looking for is actually running? Notice that `-x` looks for exact match -- maybe you are looking for `pgrep -f` actually? – tripleee Feb 15 '18 at 13:01
  • using `pgrep -x` is not listing any processes, but for `pgrep -f` its listing and i tried using the same in my script but still its not showing any processes, directly its going inside `else` and executing the jar file – Prateek Naik Feb 15 '18 at 13:04
  • If it works on the command line, the same command in a script should also work unless you are running it incorrectly (wrong server? wrong arguments? wrong shell?) ... Running the script with `bash -x` might reveal something useful for debugging. – tripleee Feb 15 '18 at 13:08
  • from command line, its working fine for me also. But when i run the script its not working. – Prateek Naik Feb 15 '18 at 13:12
  • `bash -x` its giving `++ pgrep -f ams.war` `+ apid=` `+ echo Stopped` `Stopped` `+ cd /target` `+ nohup java -jar xyz.jar` – Prateek Naik Feb 15 '18 at 13:15
  • You are looking for `ams.war` but starting `xyz.jar` – tripleee Feb 15 '18 at 13:17
  • sorry its `xyz.war` only – Prateek Naik Feb 15 '18 at 13:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165224/discussion-between-tripleee-and-ben-bean). – tripleee Feb 15 '18 at 13:32