bash 如何更改/分配 java .jar 的进程名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4650348/
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
How to change/assign process name of java .jar
提问by emacsomancer
I'm running Minecraft under Linux, which involves running an executable .jar file. This means it shows up as "java" under ps, rather than "minecraft". I would like to assign it the process name "minecraft".
我在 Linux 下运行 Minecraft,这涉及运行一个可执行的 .jar 文件。这意味着它在 ps 下显示为“java”,而不是“minecraft”。我想为其分配进程名称“minecraft”。
Looking around, I found the following tip for assigning a process name via bash:
环顾四周,我发现了以下通过 bash 分配进程名称的提示:
how to change the name of a Java application process?
exec -a goodname java ...
I usually run with:
我通常运行:
java -cp ~/Games/Minecraft/Minecraft.jar net.minecraft.LauncherFrame
So tried make a bash script:
所以尝试制作一个 bash 脚本:
#!/bin/bash
exec -a minecraft java -cp ~/Games/Minecraft/Minecraft.jar net.minecraft.LauncherFrame
But when I run this, it still shows up as "java" under the ps command.
但是当我运行它时,它仍然在 ps 命令下显示为“java”。
What am I doing wrong?
我究竟做错了什么?
回答by Victor Zamanian
It works for me. I haven't tested with java, but I tested with sleep:
这个对我有用。我还没有用 java 测试过,但我测试过sleep:
victor@vz:~$ exec -a minecraft sleep 1m &
[1] 3858
victor@vz:~$ ps x | grep mine
3858 pts/2 S 0:00 minecraft 1m
3860 pts/2 S+ 0:00 grep --color=auto mine
victor@vz:~$
However, this seems to be merely a cosmetic change as far as I can tell by the documentation:
然而,据我所知,这似乎只是一个表面上的变化:
victor@vz:~$ help exec exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...] Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program. ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified, any redirections take effect in the current shell. Options: -a name pass NAME as the zeroth argument to COMMAND
victor@vz:~$ help exec exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...] 用给定的命令替换 shell。
Execute COMMAND, replacing this shell with the specified program. ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified, any redirections take effect in the current shell. Options: -a name pass NAME as the zeroth argument to COMMAND
In reference to OP's comment to this answer: I just tested it on a remote machine with java as well:
参考 OP 对此答案的评论:我刚刚在远程机器上用 java 测试了它:
victorz@exa:~$ javac test.java # spits out an Administrator.class file among others
victorz@exa:~$ exec -a minecraft java Administrator &
[1] 13142
victorz@exa:~$ ps x | grep mine
13142 pts/1 Sl 0:00 minecraft Administrator
13161 pts/1 S+ 0:00 grep --color=auto mine
victorz@exa:~$
Maybe you are not using the xswitch to ps? I get no match unless I use the xswitch.
也许您没有使用xswitch 到ps? 除非我使用x开关,否则我无法匹配。

