linux – Run program with changed process name

Question:

How to run a program with a changed process name?

Answer:

There are two independent "process names": one is derived from the path executable itself, the other is determined by argv[0] from the command line.

Both parameters are passed to the exec*() function, which is used to run POSIX executables (by replacing the current process, most often after a fork) :

execvp(path, argv);

The exec -a bash command mentioned by @WiT changes argv[0] when calling execv*() function. In zsh , you can define ARGV0 with the same effect:

$ ARGV0=new-name your-command arg1 arg2

To change the path for the new process, you can create a link, as @avp suggested:

$ ln -s $(command -v your-command) new_name

From inside an already running process, you can call prctl() to change the "real" ( path -based process name):

prctl(PR_SET_NAME, title, 0, 0, 0); /* title is upto 16 chars */

argv[0] can also be changed from inside the process. Changing argv[0] is not guaranteed to work – PostgreSQL provides a portable implementation where you can see all the apparently horrifying details of this procedure .

You can look at the names with the ps command:

$ ps axk comm o comm,args

This command shows both types of name.

Scroll to Top