LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-07-2006, 11:08 AM   #1
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Konsole - which pseudo-tty per session?


I use KDE konsole; a lot. Right now my task bar shows 22 instances of konsole running, and most of those have at least 3 or 4 sessions open. Probably at least 100 in total. Most are running a secure shell session logged into another computer somewhere, and often I will have numerous separate sessions logged into any one remote host. When I list my processes with 'ps -e l', it shows me what pts the session started on, like this:
Code:
0  1142  4772  6451  16   0  4484  948 select S+   pts/25     1:45 ssh -L 8000:ibcs:8000 ibcs
My question is, how can I somehow reverse lookup the konsole session that the particular pts (pts/25 in the example) was started for? I want to go to that konsole session, and close it. If I go to another konsole session that is also ssh'ed to the same host and break that connection, it may be disruptive to ongoing work, so I don't want to use a brute-force method, and just disconnect everything.

I can't find anything within konsole itself that seems to identify the relationship between konsole sessions, and pts's. Are there any 'hidden' places where this might be found?

Hope this makes sense.

--- rod.
 
Old 11-07-2006, 12:37 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Where are you getting the pts ID from? Are you getting this from the host which you are sshing out of, or from the remote host?

If you're getting the pts from the local host, it's fairly easy - just send the HUP signal to the bash shell which has that pts - it will disconnect from the remote host and close the shell. You can get the process ID from the ps output which tells you the pts ID.

For example, I started a bunch of konsole sessions, and used ssh in several of them to connect to a remote host. Lets say I want to disconnect the ssh connection on pts/8:
Code:
$ ps aux |grep pts/8
matthew   9570  0.0  0.6   5784  3280 pts/8    Ss   18:18   0:00 /bin/bash
matthew   9918  0.0  0.4   4804  2088 pts/8    S+   18:25   0:00 ssh tosh
matthew  10029  0.0  0.1   2808   760 pts/5    R+   18:35   0:00 grep pts/8
All I do is send the HUP signal to the bash shell:
Code:
kill -HUP 9570
The signal is send to childred - the ssh process disconnects on getting the signal, and the shell terminates.

If I sent the signal to the ssh process instead of the bash shell, it would disconnect, but the bash shell (and that tab in konsole) would remain open.
 
Old 11-07-2006, 01:03 PM   #3
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Quote:
My question is, how can I somehow reverse lookup the konsole session that the particular pts (pts/25 in the example) was started for?
I don't think you can directly. The easiest way to achieve this is to put the tty number in the title bar. Then you can just skim down the window list to locate the relevent konsole (that's what I do, though I prefer rxvt, which is 10x faster than xterm, and that's 5x faster than any KDE bloat!! ).

edit:
You can set the title bar with:

Code:
echo -n "\e]0;<TEXT>\007"
and get the tty from, e.g.

Code:
TTY=$(tty)
TTY=${TTY#/dev/}
(re)edit:
Oh, you just want to close it, missed that at first. In that case, you can just KILL/HUP the shell, as matthewg42 demonstrated. Though if you want to switch to a given konsole (not close it), the above might be useful.

(re)(re)edit:

If you want to simplify the output of ps, try something like:

Code:
ps -U $USER -o pid,tty,comm

Last edited by soggycornflake; 11-07-2006 at 01:20 PM.
 
1 members found this post helpful.
Old 11-07-2006, 02:25 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Another way to name the tabs is via the dcop interface:
Code:
dcop "$KONSOLE_DCOP_SESSION" renameSession "My tab name"
Here's a rather convoluted way to get the konsole instance and session number using the proc filesystem and some core utils. Of course, it requires that you have the proc filesystem enabled on your system.

1. Get a list of konsole PIDs using pgrep

2. For each konsole PID, look in /proc/<PID>/fd and find symlinks to pts devices. Now you have a list of pts's for that konsole window.

3. You've now narrowed your search down to one konsole instance, but there are still multiple tabs to work through. We can use dcop, but it's a little ugly - several calls:
Code:
dcop konsole-<PID> |grep session
This will return a list of sessions (tabs). For each one, you can call the sessionPID function to find the PID of the shell running in that session.
Code:
dcop konsole-<PID> session-x sessionPID
...and then you can use ps to get the tty for that PID:
Code:
ps -p <BASH-PID> -o tty=
Once you've found your tty, you have the konsole PID (window), the konsole session ID (tab) and the bash PID. If at this point you want to find out the tab label, you can use dcop like this:
Code:
dcop konsole-<PID> session-x sessionName
Voila. It shouldn't be too hard to knock up a script to do this sort of thing. I'd probably go for perl, but it should be quite do-able with bash or tcl or python or whatever you like. The performance bottleneck will be calling dcop multiple times. It might be worth using python because there is a dcop module which means you don't have to invoke the command line dcop tool multiple times...
 
1 members found this post helpful.
Old 11-07-2006, 02:32 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
More dcop goodness... if you know the konsole ID and the session ID, you can make konsole switch to that tab using the dcop interface. An example:
Code:
dcop konsole-9437 konsole activateSession session-7
Note that this doesn't put that konsole window into the foreground - it just switches the active tab. I'm not sure how to bring the window into the foreground without using the mouse. Anyone know?
 
Old 11-07-2006, 04:39 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399

Original Poster
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Great answers, all. I haven't quite digested them all yet, but I'm pretty sure there is more than one useful solution there. What I didn't say was that I usually don't want to just drop the connection(s) ungracefully, so just killing the shell isn't usually my preferred solution, but is a solution, nonetheless. I've always wondered what dcop was good for; looks like I now have a good excuse to find out.

Thanks muchly, folks.

--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Regarding Pseudo tty, Pseudo terminals ? mqureshi Programming 0 07-30-2005 10:51 AM
managing pseudo terminals in an ssh session kirkhamster1 Linux - Newbie 1 10-08-2003 12:26 AM
Pseudo tty configuration RH 7.3 Dalma Linux - Networking 1 06-17-2003 12:53 PM
Eterm pseudo-tty? jpbarto Linux From Scratch 4 04-14-2003 11:51 AM
Pseudo-TTY Control Under Linux nitr0gen Programming 0 03-21-2002 12:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:05 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