在Linux中更改进程名 [英] Change process name in Linux

查看:701
本文介绍了在Linux中更改进程名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux和我分叉/ execing一个新的进程了我的C菌种的应用。是否有可能也会改变这些新的子进程的命名?

I'm on Linux and 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.

推荐答案

我觉得这应该工作,来说明原理...

I think this should work, to illustrate the principle...

#include <stdio.h>

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

改个名字,并把一个A,而不是第一个字母。 CtrlZ暂停,然后运行 PS 看名字改变了。我不知道,但似乎有点危险,因为有些东西可能取决于的argv [0]

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].

另外,我试图取代指针本身到另一个字符串;没有雪茄。所以,这只会与的strcpy 和字符串短或比原来的名称相同。

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.

编辑:非字面的解决方案:如果你分叉,你知道孩子的PID( GETPID()中的孩子,的结果叉( )父)。只输出它的地方在那里,你可以读取它,并通过PID杀死了孩子。

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.

另外一个非文本的解决方案:使软链接到可执行文件与另一名( LN -s a.out的kill_this_a.out ),那么当你的exec,执行的是链接。该名称将是链接的名字。

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.

这篇关于在Linux中更改进程名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆