How to change argv0 in bash so command shows up with different name in ps?

Viewed 21727

In a C program I can write argv[0] and the new name shows up in a ps listing.

How can I do this in bash?

8 Answers

ps and others inspect two things, none of which is argv0: /proc/PID/comm (for the "process name") and /proc/PID/cmdline (for the command-line). Assigning to argv0 will not change what ps shows in the CMD column, but it will change what the process usually sees as its own name (in output messages, for example).

To change the CMD column, write to /proc/PID/comm:

echo -n mynewname >/proc/$$/comm; ps

You cannot write to or modify /proc/PID/cmdline in any way.

Process can set their own "title" by writing to the memory area in which argv & envp are located (note that this is different than setting BASH_ARGV0). This has the side effect of changing /proc/PID/cmdline as well, which is what some daemons do in order to prettify (hide?) their command lines. libbsd's setproctitle() does exactly that, but you cannot do that in Bash without support of external tools.

Related