LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2021, 05:28 AM   #1
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Rep: Reputation: Disabled
Bash Question: How to get the pid of 2nd terminal tab in 1st terminal tab ?


Envirement:
* Linux like Ubuntu, Cinnamon desktop, bash, terminal

How to get to get the pid of 2nd terminal tab in 1st terminal tab ?

Follow sample:
Code:
echo pid $$; gnome-terminal --title="TAB-Titel" --tab --active -- bash -c "echo $$ & echo Hello; read exec bash"
What the sample try to do on this time (dosnt realy work on this time):
* echo the pid of 1st terminal tab on 1st terminal tab
* echo the pid of 2nd terminal tab on 2nd terminal tab

Whats wrong with the sample code?
* I am getting wrong pid as echo on 2nd terminal tab.
* I dont know how to output the pid of 2nd terminal tab on 1st terminal tab also.

Any idea how to do ?

The follow are known:

Its known its possible to get the pid of a terminal by:
Code:
echo $$
Its known its possible to get the pid of a called program by p.e. follow:
Code:
xed & $!
Its known its possible to show the bash used pids by follow:
Code:
ps ax | grep bash
Its known its possible to close terminal Tab by follow for testing purposes:
Code:
kill -15 123456

Last edited by Lennard37; 12-04-2021 at 08:47 AM.
 
Old 12-04-2021, 06:21 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Quote:
Originally Posted by Lennard37 View Post
I am getting wrong pid as echo on 2nd terminal tab.
A quoting issue
Code:
.. -- bash -c "echo $$ & echo Hello; read exec bash"
must be
Code:
.. -- bash -c 'echo $$ & echo Hello; read exec bash'
 
Old 12-04-2021, 07:09 AM   #3
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
A quoting issue
must be
Code:
.. -- bash -c 'echo $$ & echo Hello; read exec bash'
The follow are working great now:
Code:
echo pid $$; gnome-terminal --title="TAB-Titel" --tab --active -- bash -c 'echo $$ & echo Hello; read exec bash'
Now i get the follow:
* echo the pid of 1st terminal tab on 1st terminal tab
* echo the pid of 2nd terminal tab on 2nd terminal tab

THX !!!

Last edited by Lennard37; 12-04-2021 at 08:48 AM.
 
Old 12-04-2021, 08:53 AM   #4
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Original Poster
Rep: Reputation: Disabled
How to export a var from one terminal tab to a other terminal tab ?

The follow sample code are doing the follow:
* echo the pid of 1st terminal tab on 1st terminal tab
* echo the pid of 2nd terminal tab on 2nd terminal tab

Copy the follow to the terminal:
Code:
echo pid $$; gnome-terminal --title="TAB-Titel" --tab --active -- bash -c 'echo $$ & echo Hello; read exec bash'
How to output the pid from 2nd terminal tab on 1st terminal tab ?

I tryed the follow on 2nd terminal tab:
Code:
export pid_of_2nd_term_tab=$$
I tryed the follow after than on 1st terminal tab a don't get a output:
Code:
echo $pid_of_2nd_term_tab
Its looks like its not the right way to export a var from one tab to a other.

Any idea to get the pid of 2nd terminal tab available on 1st terminal tab?

Last edited by Lennard37; 12-04-2021 at 09:51 AM.
 
Old 12-04-2021, 11:00 AM   #5
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

Appears to be a duplicate?

https://www.linuxquestions.org/questions/programming-9/bash-question-how-to-get-the-pid-of-2nd-terminal-tab-in-1st-terminal-tab-4175704469

 
Old 12-04-2021, 12:49 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. Your two threads have been combined.
 
Old 12-04-2021, 01:19 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,875
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
@OP: Could you please explain what is problem X in your case?
 
Old 12-04-2021, 01:20 PM   #8
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Original Poster
Rep: Reputation: Disabled
It isn't a duplicate.
 
Old 12-04-2021, 03:58 PM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by Lennard37 View Post
It isn't a duplicate.
Your first post/thread asks:
Quote:
How to get to get the pid of 2nd terminal tab in 1st terminal tab ?
Your second one has:
Quote:
How to output the pid from 2nd terminal tab on 1st terminal tab ?
...
Any idea to get the pid of 2nd terminal tab available on 1st terminal tab?
I don't see the difference?

Also, as NevemTeve suggests - if you still have an outstanding issue - describing what the source problem is (rather than the problem with your attempt to solve it) is likely to get better answers.

 
Old 12-05-2021, 05:29 AM   #10
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
Your first post/thread asks:


Your second one has:


I don't see the difference?

Also, as NevemTeve suggests - if you still have an outstanding issue - describing what the source problem is (rather than the problem with your attempt to solve it) is likely to get better answers.
Ok. Its looks like I didnt change the headline after splitting the question in two discussion parts.
 
Old 12-05-2021, 11:15 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,875
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Kindly merge the two parts and explain what your actual problem is.
 
Old 12-06-2021, 01:17 PM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
You can get the parent pid in the child process

Code:
gnome-terminal --title="TAB-Titel" --tab --active -- bash -c 'echo "my pid is $$ and my parent is '$$' !"'
The calling shell concatenates a protected 'string' and $$ and a protected 'string'

With an exported variable everything is accessable in the child process
Code:
export PP=$$
gnome-terminal --title="TAB-Titel" --tab --active -- bash -c 'echo "my pid is $$ and my parent is $PP !"'
Vice versa, getting the pid of the child process, is harder.
You can run the child process in the background and get its pid as $!
Code:
gnome-terminal --title="TAB-Titel" --tab --active -- bash -c 'echo "my pid is $$ and my parent is '$$' !"' &
echo "my pid is $$ and my child was spawned as $! !"

Last edited by MadeInGermany; 12-06-2021 at 01:28 PM.
 
Old 12-07-2021, 05:22 AM   #13
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
You can get the parent pid in the child process
One terminal are opening a 2nd terminal tab.
I like to get the pid of 2nd terminal tab on 1st terminal tab.

Thats sounds for me like:
I try to get the child pid available on parent process.
 
Old 12-07-2021, 05:33 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,985

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Quote:
Originally Posted by Lennard37 View Post
One terminal are opening a 2nd terminal tab.
I like to get the pid of 2nd terminal tab on 1st terminal tab.

Thats sounds for me like:
I try to get the child pid available on parent process.
But unfortunately it is wrong. the tab has no pid. The apps you have are: 1. gnome_terminal, 2. shell in first tab, 3. shell in second tab, ....

When you want to open a new tab you need to "ask" the terminal to do that for you. Every shell will say: the parent process is the gnome-terminal itself.
 
Old 12-07-2021, 05:53 AM   #15
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
the tab has no pid.
Its looks for me, every terminal tab have a pid.

Open a terminal and output the pid of current running terminal tabs
pgrep bash

You get a output like
Code:
12345
After than open a additional terminal tab on already opened terminal, p.e by menu item of terminal

Run the follow on 2nd terminal tab and you get the pid of 1st and 2nd terminal tab
pgrep bash

You get a output like
Code:
12345
23456
Now its possible to check the pid of 2nd terminal tab is true or wrong
Kill the process of 2nd terminal tab, pe. by a gui driven process manager, perhaps its possible by kill on terminal to.

So you can see, the the 2nd terminal tab are closing on this way and other terminal tabs dont close on this way. Thats sounds for me, the 2`nd terminal tab have a own pid, which are on this sample is the pid 23456.

So I can do it on follow way:
* Create a list of bash pids after open the terminal (before open the 2nd terminal tab
Code:
arr1=$(pgrep bash)
echo $arr1
* Create a list of bash pids after open the 2nd terminal tab
Code:
arr2=$(pgrep bash)
echo $arr2
* doing a diff of arr1 and arr2, reports me usual the wanted pid
Code:
23456
Thats should work mostly, a have little bit the the risk to get more than one pids as a diff, if other bash terminal are starting at the same small time frame, depend on what ever are running on same system. Thats should be not a big risk. A the risk are bigger than 0. Perhaps, they are a way, without this risk.

Last edited by Lennard37; 12-07-2021 at 06:43 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The first browser to introduce a second level in the tab bar for managing tab groups: Two-Level Tab LXer Syndicated Linux News 0 03-01-2021 05:50 AM
My 2nd attempt at a 1st post - the 1st attempt got snafued somehow hawkfeather LinuxQuestions.org Member Intro 1 07-13-2013 04:24 AM
Installing a distro 2nd drive (1st drive is XP) fails, just get flashing cursor wikapuki Linux - Newbie 31 07-03-2009 09:12 AM
Put a 2nd kanotix at hdb 3/,-4/home. 1st is at hdb8/,-9/home.#2 is using 1st's /home sleekmason Linux - General 3 12-09-2006 09:21 AM
ERROR: Couldn't write pid to pid file lawrencegoodman Linux - Newbie 2 02-13-2004 08:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration