Jump to content

Opening tabs in konsole from script

Recommended Posts

Hi all!

Here's a hard one :-)

How do I start multiple tabs in one (!) konsole from a script? I looked everywhere, but didn't find a solution.

if i do

konsole -e command1 &
konsole -e command2 &

I get two seperate konsole windows, not two tabs in one window...


And "konsole -e command1 -e command2" doesn't work, too.


And to make the question more complex:

How do I start multiple tabs with some being root and some being user while only entering the root-pw once... Why? Because I got a loooong and secure root pw and when I login on our company server I want to have "tail -f" on about 5 logs as root, have one root shell to work and two user shells to work as user... :-) And it would be nice to have all those terminals as tabs in a konsole (or any other terminal that supports tabs)


Or - if it won't work in a script: how do I spawn a new tab from the current one with a command (keeping the user! As I mentioned, I wouldn't like to re-enter my root pw that often...)


Thanks for your help.


Panic. (No, not you, me.)

Link to comment
Share on other sites

Not sure if this will help but in konsole, if you go into Settings>Configure shortcuts you can set all kinds of keyboard shortcuts. The one for opening a sessinon in a new tab is Alt+Ctrl+N. I don't know how you can work that into a bash script. However, since you are using konsole, I assume your running kde. I'm not familiar with it, but I'm fairly certain you can do what you want using "dcop" in a bash script. Check out this article for an intro to this subject:




Read through the article, then open konsole and run:


$ kdcop


which will open the kdcop browser window. Go down to konsole-27334>konsole (default)>QString newSession


By double clicking on the method "newSession", new tabs are opened up in konsole. I assume you can call this method in a script as described in the above article. For example, I ran this from the command line and opened two new console tabs:


$ dcop konsole-27334 konsole newSession && dcop konsole-27334 konsole newSession



Sticking that in a bash script should do the same. Problem is that every time I open up a new konsole, I get a different series of digits after "konsole-" and running w/o those digits doesn't work. Your bash script would have to open konsole and run:


dcop | grep konsole


and assign the output to a variable and use that in your dcop command. I think if you fool around with it for a while you can get just what yopu want with dcop.

Link to comment
Share on other sites

Thanks, I'll try. :thanks:

I just hoped there'd be an easier way :-) Like the mozilla-remote scripts that open a website in an open mozilla-window...


Couldn't there be a "konsole --reuse-window --newtab -e command" command? :-)




Edited by PeterPanic
Link to comment
Share on other sites

OK... I've come so far to this code:


#konsole must have been started with the option "--script"
#if I didn't, I couldn't use "sendSession"...

# get the root pw in a variable
password=$(kdialog --password "Please enter the ROOT password:")

# get last opened konsole in variable $kon
kon=$(dcop "konsole*" | tail -1)

# open a new tab and assign its name to $tab
tab=$(dcop $kon konsole newSession)

# now I can use the variables and the "sendSession" command.
dcop $kon $tab sendSession "echo Hello, World!"


This works! Thanks to your help. I wouldn't even have found dcop at all.


BUT: Now I opened a tab as a normal user. I'd like to become root in that tab and open the syslog as root. The bad thing is that you can't echo a variable to su... su always wants to get input from a tty, so that I'd have to enter the root pw again and again (I want to repeat the last three commands for various log files each in a new tab as root.)


Closing the log (Ctrl+C) should close the tab, as usual. It would have to be a command like

echo $password > su -c "tail -60f /var/log/syslog"

but, as I said, that won't work.


Can someone help again?


BTW: I'm using XFce4, not KDE, but konsole and dcop work fine without KDE. dcop works with all running KDE applications... and konsole is one :-)



Link to comment
Share on other sites

  • 4 years later...
Guest vjain

Hi Peter,


I was trying to do the same thing and your post was pretty useful in suggesting a way to open a new tab programatically.


However, for inputing the root password multiple times, you can use the konsole feature of "Send input to all sessions"


This way, if you type on a single session, all the typed characters are reflected in the other shells.


Hope this helps


Link to comment
Share on other sites

  • 7 months later...



I saw the earlier post regarding 'dcop' and played around with that a bit, resulting in something that may be useful for doing what you want, in combination with a couple of other things.


First, some notes on 'dcop' about what I learned:


dcop with no args prints a list of applications currently running. Some examples are: kwin kicker konsole-651


dcop konsole\* # prints just the konsole apps that are running. The number is the PID of the particular konsole.


dcop konsole-651 konsole # prints functions that can be called for the particular application. Paritcularly, I noticed both newSession() and newSession(QString type). And I remembered ...


There is session management in konsole, in particular you can go to "Settings->Configure Konsole" and create new, named sessions, that run specific commands locally or remotely (ssh, for example). The command can be any program that can be found in your PATH, or an absolute path to something, so long as it is "executable". Including using 'su' or 'sudo' to start root level operations.


You could use 'sudo', configured to not require a password for a particular user and particular command set (for example, cat and tail only), and configure a session called, for example, chkvarlog.


You can run this session manually by clicking the 'Session' menu and selecting the name, or by click/holding the open a new session tab button.


Or, you could use dcop to to open the tab for you: dcop konsole-651 konsole newSession chkvarlog


You could then put this all into a script:


1. start 'konsole' in the background with the session you created: konsole --type chkvarlog &

2. Or, start a konsole normally, and trigger the new session with: dcop konsole-7743 konsole newSession chkvarlog


You would need to figure out, in the second case, how to determine which console you want. When I ran the 'dcop konsole\*' the first time, I got the list in reverse order, the second time (with a different invocation, to be sure), the order was "correct". You could try running through a numeric sort, but that could break when PID numbers start recycling (max PID is 32K, in case you didn't know ;).


I don't know if my ramblings will be helpful for your specific needs. I hope they at least help get you closer to your goal.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...