How to get the process id for a java process on the command line in FreeBSD, sh

I need help writing a command to be placed in .sh. I want to return the process ID that is below 3678 in the output, but I have diffuclty because the process ID changes every time it restarts, so my code is interrupted

Conclusion:

[root@server1 /usr/home/aaron]# ps -xauww | grep java | grep www
www      3678  0.0  3.2 1308176 267864  ??  Is    3:21PM   0:17.19 [java]
[root@server1 /usr/home/aaron]#

Here is what I did until I realized that the column changed when the pid changed:

ps -xauww | grep java | grep www | cut -d" " -f6

Any help is appreciated. thank.

+3
source share
4 answers

One way can be found in: man pgrep

0
source

script, pid , $!.

java ...... &
echo "$!" > myjavaprogram.pid

, :

kill `cat myjavaprogram.pid`
+12

pgrep pid jar :

pgrep -f test-app.jar

+2

http://cfajohnson.com/shell/cus-faq-2.html

  • ? , , ?

    . , . , pid, . , pid , , .

    ps, , pid, , , - -. script, , , .

    - pgrep, Solaris, 'ps h -o pid -C $STRING ', , ps , .

    C pid , fcntl (2). , , , . , .

    We do not have such capabilities in the shell if we cannot provide a C program that can try locking for a script. Despite this, the race condition described above still exists.

Take a look at this: http://www.faqs.org/faqs/unix-faq/faq/part3/section-10.html can this help you?

+1
source

Source: https://habr.com/ru/post/1776569/


All Articles