8

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".

Looking around, I found the following tip for assigning a process name via 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:

#!/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.

What am I doing wrong?

Community
  • 1
  • 1
emacsomancer
  • 597
  • 1
  • 5
  • 14
  • `exec -a` works for me on Ubuntu 8.04, but it still displays all the java arguments ("-cp" and so on) and killall wouldn't find the process by its new name, only `killall java` worked. You may also wish to use binfmt to run JAR files directly from the command line. – Sergei Tachenov Jan 10 '11 at 19:06
  • Does binfmt allow changing the process name? And, if so, how would I use it in this case? – emacsomancer Jan 10 '11 at 19:15
  • Oh, my bad, using binfmt it still shows as java. Moreover, "exec -a" stops working for it too. So this is actually not a solution, but the exact opposite of one. – Sergei Tachenov Jan 10 '11 at 19:49
  • exec -a do not work under centos7. – Kirby Zhou Nov 16 '21 at 03:09

1 Answers1

2

It works for me. I haven't tested with java, but I tested with 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

In reference to OP's comment to this answer: I just tested it on a remote machine with java as well:

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 x switch to ps? I get no match unless I use the x switch.

Victor Zamanian
  • 3,100
  • 24
  • 31
  • I tried it with sleep, and it worked just as you said. But it doesn't seem to work with minecraft/java, and I'm not sure why. [edit: I just want the cosmetic change, so that it shows up as "minecraft" with ps.] – emacsomancer Jan 10 '11 at 19:16
  • @B. Slade, maybe your "java" is a kind of wrapper shell script that launches the java binary? So it gets new process name, but then launches java and gets another new name ("java") because of that. Like it happens with binfmt. Try running java binary directly, like /wherever/your/jvm/is/bin/java. – Sergei Tachenov Jan 10 '11 at 19:52
  • @Victor: Yes, if I use the -x switch, it works as you said. The problem is that I want conky to be able to see it, and there doesn't seem to be any way of passing the switch to conky's top command. – emacsomancer Jan 10 '11 at 20:59
  • @Sergey Tachenov: It looks like my "java" may indeed be some sort of wrapper shell script itself. However, when I try to run the program directly, I get errors, starting with "java.lang.NoClassDefFoundError". – emacsomancer Jan 10 '11 at 21:02
  • @B. Slade, then look at your wrapper. It probably has some additional parameters for java to work correctly. Maybe some classpath. Basically you need to run java with the same parameters as the wrapper does (that's why it exists in the first place). – Sergei Tachenov Jan 11 '11 at 07:17
  • @Sergey Tachenov, thanks. I tried running java directly with the same parameters as the wrapper, and while it runs fine, it still appears as "java" with ps (without the x option). Oh well. It's obviously not really a major problem; I just thought there should be a way to do it. Thanks again. – emacsomancer Jan 11 '11 at 15:57
  • 1
    @B. Slade, I just checked, and without x option it doesn't work indeed. I thought the x option was only for disabling terminal filtering. I believe there still should be a way of doing this, probably involving some machinations with /proc fs. – Sergei Tachenov Jan 15 '11 at 19:48