Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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?
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.
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.
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...
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.