LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Konsole - which pseudo-tty per session? (https://www.linuxquestions.org/questions/linux-software-2/konsole-which-pseudo-tty-per-session-499449/)

theNbomr 11-07-2006 11:08 AM

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.

matthewg42 11-07-2006 12:37 PM

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.

soggycornflake 11-07-2006 01:03 PM

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

matthewg42 11-07-2006 02:25 PM

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...

matthewg42 11-07-2006 02:32 PM

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?

theNbomr 11-07-2006 04:39 PM

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.


All times are GMT -5. The time now is 06:14 AM.