Linux 如何在freeBSD的命令行中获取java进程的进程ID,sh

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4261661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:05:12  来源:igfitidea点击:

how to get the process id for a java process at the command line in freeBSD, sh

javalinuxbashunixshell

提问by Ronedog

I need some help writing a command that will be put into a .sh. I want to return the process id, which in the output below is 3678, but I'm having diffuclty because the process id changes everytime it gets restarted, so my code breaks

我需要一些帮助来编写将放入 .sh 的命令。我想返回进程 id,在下面的输出中是 3678,但我有困难,因为进程 id 每次重新启动时都会更改,所以我的代码中断

Output:

输出:

[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]#

Heres what I was doing until I realized the column changed when the pid changed:

这是我在做的事情,直到我意识到当 pid 改变时列也改变了:

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

Any help is appreciated. thanks.

任何帮助表示赞赏。谢谢。

采纳答案by jilles

One way can be found in: man pgrep

可以在以下位置找到一种方法: man pgrep

回答by Romain Hippeau

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

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

  1. How do I get a process id given a process name? Or, how do I find out if a process is still running, given a process ID?

    There isn't a reliable way to to this portably in the shell. Some systems reuse process ids much like file descriptors. That is, they use the lowest numbered pid which is not currently in use when starting a new process. That means that the pid you're looking for is there, but might not refer to the process you think it does.

    The usual approach is to parse the output of ps, but that involves a race condition, since the pid you find that way may not refer to the same process when you actually do something with that pid. There's no good way around that in a shell script though, so be advised that you might be stepping into a trap.

    One suggestion is to use pgrep if on Solaris, and 'ps h -o pid -C $STRING' if not, and your ps supports that syntax, but neither of those are perfect or ubiquitous.

    The normal solution when writing C programs is to create a pid file, and then lock it with fcntl(2). Then, if another program wants to know if that program is really running, it can attempt to gain a lock on the file. If the lock attempt fails, then it knows the file is still running.

    We don't have options in the shell like that, unless we can supply a C program which can try the lock for the script. Even so, the race condition described above still exists.

  1. 如何获得给定进程名称的进程 ID?或者,如果给定进程 ID,如何确定进程是否仍在运行?

    没有一种可靠的方法可以在 shell 中便携地做到这一点。一些系统重用进程 ID 很像文件描述符。也就是说,它们在启动新进程时使用当前未使用的最低编号 pid。这意味着您要查找的 pid 就在那里,但可能不是您认为的进程。

    通常的方法是解析 ps 的输出,但这涉及竞争条件,因为当您实际使用该 pid 执行某些操作时,您以这种方式找到的 pid 可能不会引用相同的进程。但是,在 shell 脚本中没有好的方法可以解决这个问题,因此请注意,您可能会陷入陷阱。

    一个建议是在 Solaris 上使用 pgrep,如果不是,则使用 'ps h -o pid -C $STRING',并且您的 ps 支持该语法,但这些都不是完美的或无处不在的。

    编写C程序时的正常解决方案是创建一个pid文件,然后用fcntl(2)锁定它。然后,如果另一个程序想知道该程序是否真的在运行,它可以尝试锁定该文件。如果锁定尝试失败,则它知道文件仍在运行。

    我们在 shell 中没有这样的选项,除非我们可以提供一个可以尝试锁定脚本的 C 程序。即便如此,上述竞争条件仍然存在。

That being said look at this: http://www.faqs.org/faqs/unix-faq/faq/part3/section-10.htmlit might help you out ?

话虽如此,请看这个:http: //www.faqs.org/faqs/unix-faq/faq/part3/section-10.html它可能对你有帮助吗?

回答by shellholic

If the starting is automated by a shell script, you can write the pid of the just-started-process which is in the variable $!.

如果启动是由 shell 脚本自动启动的,您可以将刚刚启动的进程的 pid 写入变量 中$!

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

When you need to kill it, just do:

当您需要杀死它时,只需执行以下操作:

kill `cat myjavaprogram.pid`

回答by Shams

Below pgrepcommand works for getting pid by jar-file name:

以下pgrep命令适用于通过 jar 文件名获取 pid:

pgrep -f test-app.jar

pgrep -f test-app.jar