Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting pid of a background gnome-terminal process

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?

like image 473
db42 Avatar asked Jul 13 '11 12:07

db42


2 Answers

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
like image 153
Osmund Avatar answered Sep 23 '22 01:09

Osmund


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.

like image 22
Sodved Avatar answered Sep 20 '22 01:09

Sodved