Giving a process a specific name in GNU/Linux?

Solution 1:

Creating a symlink should do the trick, however, it would be more helpful if we knew the disease and not the symptom. What exactly are you trying to do? Because there may be a better way

gnuix@defiant)-(08:11pm-:-06/10)--
(~)./0012
my weird program name
  PID TTY          TIME CMD
 7805 pts/1    00:00:00 zsh
14020 pts/1    00:00:00 0012
14021 pts/1    00:00:00 ps
(gnuix@defiant)-(08:11pm-:-06/10)--
(~)ln -s 0012 weird
(gnuix@defiant)-(08:12pm-:-06/10)--
(~)./weird 
my weird program name
  PID TTY          TIME CMD
 7805 pts/1    00:00:00 zsh
14046 pts/1    00:00:00 weird
14047 pts/1    00:00:00 ps
(gnuix@defiant)-(08:12pm-:-06/10)--
(~)

Update: Based on the extra information you could (I'm sure there is an easier way but its not comming to me at the moment) have your servers write out their PID to a file upon startup, then you could kill -9 | cat /var/run/devserver.pid but then that would be a programming question :)

Update again: You could also do some shell trickery to get the PID of the servers when you launch them, off the top of my head you could create a shell function called startmyserver for example that would wrap the command you use to start your servers but also capture $! to a file based upon startup name -- then we are talking about shell scripting which is 'inbounds' @ ServerFault. :-)

Solution 2:

You could rename the binary you're going to run. You might get away just using a symlink to the binary.

If you really want to change the name, the best way is to use a simple wrapper that sets argv and then execs the process you want to run. Don't have time to give you some example code now, but shout loudly in about 10 hours and I'll see what I can do.

Solution 3:

Look at https://github.com/electrum/procname. It allows you to set the process name of any process using an anvironment variable.

Usage:

Run Java with the LD_PRELOAD and PROCNAME environment variables set:

LD_PRELOAD=/path/to/libprocname.so PROCNAME=hello java -jar foo.jar

Solution 4:

This doesn't really belong on SO because it's generic, not a language specific question.

The short answer is not easily. That is, it can't be done from within the functionality of your shell or procfs/sysfs. You will need to use an external utility to achieve it.

There is a long description on how and why here. Beware it's pretty C-centric.

Update:

They're python which changes the game. Without now being too SO, it would probably be easier to modify them to make use of this module or a similar approach.