7

我可以轻松地启动一个后台进程,找到它的 pid 并在正在运行的进程列表中搜索它。

$gedit &
$PID=$!
$ps -e | grep $PID

这对我有用。但是如果我启动 gnome-terminal 作为后台进程

$gnome-terminal &
$PID=$!
$ps -e | grep $PID

然后,在所有正在运行的进程列表中找不到它。

我在这里错过了什么吗?

4

3 回答 3

13

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:

#!/bin/bash
echo "opening a new terminal"
gnome-terminal --disable-factory &
pid=$!
echo "sleeping"
sleep 5;
echo "closing gnome-terminal"
kill -SIGHUP $pid
于 2011-07-28T15:05:03.503 回答
6

这似乎是因为您启动的 gnome-terminal 进程本身启动了一个进程然后退出。因此,您捕获的 PID 是“存根”进程的 pid,该进程启动然后分叉真实终端。它这样做是为了使它可以与呼叫终端完全分离。

不幸的是,我不知道有什么方法可以捕获剩下运行的“granchild”gnome-terminal 进程的 pid。如果您执行 ps,您将看到 gnome-terminal“孙子”进程以 1 的父 pid 运行。

于 2011-07-13T12:39:52.410 回答
1

(这只是一个脚注)正如@Sodved 所说, gnome-terminal 自己启动一个进程然后退出,没有办法获得孙子 pid。(另见 APUE 第 7 章为什么当父进程终止时子进程不会重新连接到祖父进程。)

我发现 gnome-terminal 只实例化一次,所以这里只是一个针对您的特定任务的简短脚本:

GNOME_TERMINAL_PID=`pidof gnome-terminal`

如果您没有 pidof:

GNOME_TERMINAL_PID=`grep Name: */status | grep gnome-terminal | cut -d/ -f1`
于 2011-07-13T13:22:23.543 回答