This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Scripting konsole using dbus: question on current session

Tags: konsole konsole konsole
(comma "," separated)
User avatar
dbbd
Registered Member
Posts
3
Karma
0
OS
After reading http://www.linuxjournal.com/content/start-and-control-konsole-dbus#comment-352588
I got to the following essence of a script:
Code: Select all
 
        konsole --profile Node &
        sleep 0.1
        kons=$(qdbus | grep konsole | tail -1)

        tab=$(qdbus $kons /Konsole currentSession)
        qdbus $kons /Sessions/${tab} setTitle 1 "new konsole first tab"
        qdbus $kons /Sessions/${tab} sendText "ssh somewhere"
        qdbus $kons /Sessions/${tab} sendText $'\n
       
        # repeat as many time you like
        tab=$(qdbus $kons /Konsole newSession)
        qdbus $kons /Sessions/${tab} setTitle 1 "somewhere"
        qdbus $kons /Sessions/${tab} sendText "ssh somewhere"
        qdbus $kons /Sessions/${tab} sendText $'\n'


The problem is figuring out the correct konsole session. Apparently this does not always work:
Code: Select all
kons=$(qdbus | grep konsole | tail -1)

often it gives me the current konsole session, the one that invoked the script.

So I'd like to rule the current konsole session out, and loop over the grep until I get a session different from the invoking session.
What I don't know is how to figure out the current session.
ny ideas on figuring out the current session (pid of current konsole instance), or some other way to figure out the konsole session I just open
ed?

Thanks,
Dan
User avatar
dbbd
Registered Member
Posts
3
Karma
0
OS
So many replied go me to the following solution. It is far from perfect, but it works some of the time (about 50%)

Code: Select all
    konsole --profile Node 2>/dev/null &
    kpid=$(($! + 2))

    sleep 0.8
    kons=$(qdbus | grep $kpid 2>/dev/null)
    if [ -z "$kons" ]; then
        kdialog --sorry "got wrong kons: expected $kpid got $(qdbus | grep konsole)"
            kill $kpid
        return
    fi

I open a new konsole window, and take its pid. I noticed the konsole session is usually that pid+2.
I now check if qdbus has a konsole session with that pid, and if not, I issue an error and [attempt] to kill the new konsole window [the kill never works, and I don;t know why yet].
User avatar
Jekyll
KDE Developer
Posts
34
Karma
0
OS
I guess something like this is better than the hack of guessing pid:

Code: Select all
before=`mktemp`
qdbus | grep konsole | sort > $before

konsole --profile Node 2>/dev/null &
sleep 0.8

after=`mktemp`
qdbus | grep konsole | sort > $after

kons =`comm -3 $before $after`
chrishess
Registered Member
Posts
1
Karma
0
If you always want to get the latest konsole pid...

pidof konsole -> will return the list of pids of all the konsoles

ie.
[prompt] pidof konsole
36182 6603 4074

then you can determine the age of the processes with ps
ps -o etimes= -p 4074
133
ps -o etimes= -p 6603
2458
ps -o etimes= -p 36182
172

so from this, i know that 6603 is the first konsole i opened, 36182 is the second konsole and 4074 is the third konsole

so in the latest konsole you know 4074 is the pid to use...


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], Sogou [Bot]