How to run a program with a modified process name?

  • one
    Are you interested in changing the name in the ps command output? As far as I understand, this is the contents of the file / proc / PID / comm and you cannot write anything to it yourself. You can try to make a link with a different name to your argv [0] and execute exec with this file, restarting yourself in the same process, but under a new name (it will appear in / proc / PID / comm, the contents of / proc / PID / cmdline too will change according to exec arguments). - avp pm
  • association: stackoverflow.com/questions/3251550/… - Nicolas Chabanovsky

3 answers 3

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

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

 execvp(path, argv); 

exec -a command in bash, mentioned @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 a new process, you can create a link, as @avp suggested:

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

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

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

argv[0] can also be changed from the inside of the process. Changing argv[0] not guaranteed to work - PostgreSQL provides a portable implementation in which you can see all the obvious horrific details of this procedure .

You can look at the names with the help of the ps command:

 $ ps axk comm o comm,args 

This command shows both types of name.

    In Bash, this is done like this: exec -a NamesWe want Executable File Arguments

       $ perl -e "\$0='VErrrry Cewl NaMe';while(<STDIN>){}" 

      AND:

       $ ps fax | grep Cewl 28193 pts/9 S+ 0:00 | | | \_ VErrrry Cewl NaMe