Tigran84
Tigran84

Reputation: 193

How to get PID of terminal inside script which is created gnome-terminal command, but keeping terminal active for later use

Here is my command

gnome-terminal --working-directory ~ -- bash -c " echo 'Execution started' && ./test && echo 'Execution finished' ; /bin/bash"&

#!/bin/bash
echo "PPID ="
echo $PPID

I use in the end of my command /bin/bash to create new terminal and keep it opened for later use, but I need to store PID of terminal which is runs my script and not close it, gnome-terminal command always closed terminal which executes my ./test script

Upvotes: 0

Views: 691

Answers (1)

Arnaud Valmary
Arnaud Valmary

Reputation: 2325

I understand, you want the PID of the terminal where you lauch a new terminal.

So:

gnome-terminal --working-directory ~ -- bash -c "FATHER_TERM_PROC=$PPID /bin/bash" &

In the new terminal:

$ echo PID=$$, PPID=$PPID, FATHER_TERM_PROC=$FATHER_TERM_PROC
PID=5166, PPID=5159, FATHER_TERM_PROC=4417

$ ps -o pid,ppid,args -p $$ -p $PPID $FATHER_TERM_PROC
    PID    PPID COMMAND
   4417    2043 /usr/bin/xfce4-terminal --geometry=125x40
   5159    1600 /usr/libexec/gnome-terminal-server
   5166    5159 /bin/bash

Now, you known, my first terminal is a XFCE terminal!

Is this what you wanted?

Upvotes: 1

Related Questions