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
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 3
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 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.
Related videos on Youtube
Alex
Updated on September 17, 2022Comments
-
Alex over 1 year
How do I launch a process so that it would have a specific identifier in
ps
command?UPD: I have a couple of servers running on one box. Most of them are in deployment mode. However one is in development. All servers are Pythonic and the disease is that all the servers are listed as
python
inps
, which makes it impossible tokillall
the one in development leaving others intact.UPD: As David and GNUix suggested I created symlinks to Python interpreter and bash scripts for each server.
-
Toby almost 15 yearsexactly my point. You need code for that (aside from renaming/(sym)linking the binary which could be problematic if it checks for argv[0])
-
deathly809 almost 15 yearstouche.. I knew there was an easier way :)
-
David Pashley almost 15 yearsThe C code is considerably more verbose than it needs to be due to the amount of outputting it does. It should be possible to shrink it down, but it's doing pretty much the same as I was suggesting. Only with some code to go with it :) The bit it's missing is the exec to run your code :)
-
nhed about 9 yearsSome binaries won't work well if you do mostly embedded (busybox, dropbear)