Skip to content
Advertisement

Change process name in Linux

I’m on Linux and I am forking/execing a new process out of my C spawn application. Is it possible to also change the naming of these new child processes?

I want to be able to identify the process being started in case something goes wrong and I need to kill it manually. Currently they all have the same name.

Advertisement

Answer

I think this should work, to illustrate the principle…

#include <stdio.h>

int main(int argc, char *argv[]) {
  argv[0][0] = 65;
  sleep(10);
}

will change the name, and put an “A” instead of the first letter. CtrlZ to pause, then run ps to see the name changed. I have no clue, but it seems somewhat dangerous, since some things might depend on argv[0].

Also, I tried replacing the pointer itself to another string; no cigar. So this would only work with strcpy and strings shorter or equal than the original name.

There might or might not be a better way for this. I don’t know.

EDIT: nonliteral solution: If you’re forking, you know the child’s PID (getpid() in the child, result of fork() in the parent). Just output it somewhere where you can read it, and kill the child by PID.

another nonliteral solution: make softlinks to the executable with another name (ln -s a.out kill_this_a.out), then when you exec, exec the link. The name will be the link’s name.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement