Getting the pid of the background of the gnome-terminal process

I can easily start the background process, find its pid and find it in the list of running processes.

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

This works for me. But if I run the gnome terminal as a background process

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

Then it is not found in the list of all running processes.

Did I miss something?

+6
source share
3 answers

It seems that the gnome-terminal startup process starts the process itself and then terminates. Thus, the PID you are shooting is the pid of the “stub” process that starts, and then the plugs of the real terminal. He does this so that he can be completely disconnected from the calling terminal.

Unfortunately, I do not know of any way to capture the pid of the gnome-terminal "granchild" process, which remains one of them. If you make ps, you will see the gnome-terminal grandson process with a parent pid of 1.

+5
source

If you use the --disable-factory option for gnome-terminal, you can use the gnome-terminal as you wish. By default, it tries to use an already active terminal, so this will allow you to grab the pid of the one you are running. The following script opens a window for 5 seconds and 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 
+9
source

(This is just a footnote). As @Sodved said, gnome-terminal starts the process itself and then exits, there is no way to get pid grandchild. (See also APUE Chapter 7, why the child process will not rejoin the grandparent process when its parent process was interrupted.)

I found that gnome-terminal creates an instance only once, so here is just a short script for your specific task:

 GNOME_TERMINAL_PID=`pidof gnome-terminal` 

If you do not have pidof:

 GNOME_TERMINAL_PID=`grep Name: */status | grep gnome-terminal | cut -d/ -f1` 
+1
source

Source: https://habr.com/ru/post/892676/


All Articles