Search code examples
javacommand-line-interfaceraspbianapplication-shutdown

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?


Solution

  • If you prefer a one-liner

    JPID=$(jps -m | grep NameOfTheMainClass  | awk '{print $1}') && [ -z "$JPID" ] || kill $JPID