获取后台 gnome 终端进程的 pid
我可以轻松启动后台进程,找到其 pid 并在正在运行的进程列表中搜索它。
$gedit &
$PID=$!
$ps -e | grep $PID
这对我有用。但是如果我启动 gnome-terminal 作为后台进程
$gnome-terminal &
$PID=$!
$ps -e | grep $PID
那么,在所有正在运行的进程列表中找不到它。
我在这里错过了什么吗?
I can easily start a background process, find its pid and search it in the list of running processes.
$gedit &
$PID=$!
$ps -e | grep $PID
This works for me. But if I start gnome-terminal as the background process
$gnome-terminal &
$PID=$!
$ps -e | grep $PID
Then, it is not found in the list of all running process.
Am I missing something here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎是因为您启动的 gnome-terminal 进程本身启动了一个进程,然后退出。因此,您捕获的 PID 是“存根”进程的 pid,该进程启动然后分叉真实终端。这样做是为了与调用终端完全分离。
不幸的是,我不知道有什么方法可以捕获仍在运行的“granchild”gnome 终端进程的 pid。如果你执行 ps 操作,你将看到 gnome 终端“孙子”进程正在运行,父进程 pid 为 1。
This appears to be because the gnome-terminal process you start starts a process itself and then exits. So the PID you capture is the pid of the "stub" process which starts up and then forks the real terminal. It does this so it can be completely detached from the calling terminal.
Unfortunately I do not know of any way of capturing the pid of the "granchild" gnome-terminal process which is the one left running. If you do a ps you will see the gnome-terminal "grandchild" process running with a parent pid of 1.
(这只是一个脚注)正如@Sodved所说,gnome-terminal本身启动一个进程然后退出,没有办法获取孙子pid。 (另请参阅 APUE 第 7 章,为什么子进程在父进程终止时不会重新附加到祖父进程。)
我发现 gnome-terminal 仅实例化一次,因此这里只是一个针对您的特定任务的简短脚本:
如果你没有 pidof:
(This is just a footnote) As @Sodved said, gnome-terminal starts a process itself and then exits, there is no way to get the grandchild pid. (See also APUE Chapter 7 why a child process won't re-attach to the grandparent process when its parent process was terminated. )
I found that gnome-terminal instantiates only once, so here is just a short script for your specific task:
If you don't have pidof:
如果您对 gnome-terminal 使用“--disable-factory”选项,则可以按照您想要的方式使用 gnome-terminal。默认情况下,它会尝试使用已经活动的终端,因此这将允许您获取您启动的终端的 pid。以下脚本打开一个窗口 5 秒钟,然后终止它:
If you use the "--disable-factory" option to gnome-terminal it's possible to use gnome-terminal in the way you desire. By default it attempts to use an already active terminal, so this would allow you to grab the pid of the one you launch. The following script opens a window for 5 seconds, then kills it: