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

How to keep the window interactive after executing command?

Tags: None
(comma "," separated)
ganji.chetan
Registered Member
Posts
7
Karma
0
OS
Hello KDE Users,

I have one simple bash script named konsole.sh. Its contents are as below.

# start
konsole --new-tab --hold -p tabtitle=Server -e ls &
konsole --new-tab --hold -p tabtitle=Client
# end

When I run this script, it opens two terminal tabs in a window.
Tab named Server looses its interactivity while the Client does not.

Also, every time you run the konsole.sh file the tabs are opened in different order i.e. Server, Client or Client, Server

1. Is there a wayy to run a command in the Server tab and still keep it interactive?
2. Is there a way to open the tabs in the same order, everytime I run the konsole.sh file (Always in Server, Client) ?

Thanks in advance

Regards.
airdrik
Registered Member
Posts
1854
Karma
5
OS
Having interactivity depends on what you are launching in konsole. Konsole merely provides a text-based UI for the command you are running. When launched without a specific command to run it will run the default shell (for the default or specified profile; usually bash) which should provide the interactive environment you are expecting and getting in your "client" tab. When launched with a specific command to run then it will run that command to completion and then either close or display the output of the command and then sit and wait for you to close it if you give it the --hold option.
For running a command followed by opening a bash prompt, you could use a command like the following: bash -c "firstCommand; bash".

For opening your multiple tabs in a consistent order, you might try using a tabs file as described in the Konsole docs for --tabs-from-file.


airdrik, proud to be a member of KDE forums since 2008-Dec.
ganji.chetan
Registered Member
Posts
7
Karma
0
OS
Hey Airdrik,
Thanks, your solution helped me, but not completely.
Here's the actual bash script

konsole --new-tab --hold -p tabtitle=Angular --workdir=$ANGULAR &
konsole --new-tab --hold -p tabtitle=Angular-Server --workdir=$ANGULAR -e bash -c "npm start; bash" &
konsole --new-tab --hold -p tabtitle=Django --workdir=$DJANGO &
konsole --new-tab --hold -p tabtitle=Django-Server --workdir=$DJANGO -e bash -c "python3 manage.py runserver; bash"

If I press ctrl + c to close the Django-Server, it keeps the tab interactive. One less problem to solve, thanks to you.

But it does not work with the Angular-Server.
I dont know why it does not work for Angular-Server.

Do you know? Do you know the solution?


I also tried the below as you have suggested. It opens the tabs in the same order every time I run the bash script. In a sense it solves the original issue.
But it has few issues.

# konsole --tabs-from-file file

# entries in the filename called file
# title: Angular-Server;; workdir: /home/$USER/Desktop/Projects/Odin-angular ;; profile: default ;; command: npm start ;;
# title: Angular;; workdir: /home/$USER/Desktop/Projects/Odin-angular ;; profile: default ;;
# title: Django-Server;; workdir: /home/$USER/Desktop/Projects/Odin-django ;; profile: default ;; command: python3 manage.py runserver ;;
# title: Django;; workdir: /home/$USER/Desktop/Projects/Odin-django ;; profile: default ;;


Issues -
1. Running the script, opens a new tab on with workdir=~/Desktop, which I am not specifying. IDK how it is opening.
2. Cant run multiple commands in the command option of the file, what would be the equivalent of bash -c "python3 manage.py runserver; bash" ?
How to run multiple commands by using the --tabs-from-file option ?
3. ctrl + c is used to stop the angular and django server.
# If I press ctrl + c on Angular-Server window looses its interactivity.
# If I press ctrl + c on Django-Server, the konsole tab is closed.


Is there a solution for above issues?

Thanks in advance.
airdrik
Registered Member
Posts
1854
Karma
5
OS
the first thing that comes to mind is the extra "&" at the end, which it may be interpreting as run-in-the-background?

For using the tabs file, you'd still need to specify the command as e.g. command: bash -c "npm start; bash" (running the first command followed by bash still required to get the interactivity after closing the first command).

I'm not sure what's going on with the workdir option. Are you specifying it using that $USER variable or are you just using that as a placeholder when posting? (I would assume you should be able to use tilde paths e.g. ~/Desktop/Odin-angular, otherwise you might just need to use a fully explicit path e.g. /home/youruser/Desktop/Odin-angular)


airdrik, proud to be a member of KDE forums since 2008-Dec.
ganji.chetan
Registered Member
Posts
7
Karma
0
OS
Hi Airdrik,

the first thing that comes to mind is the extra "&" at the end, which it may be interpreting as run-in-the-background?

IDK, I am completely new to all of this.

For using the tabs file, you'd still need to specify the command as e.g. command: bash -c "npm start; bash" (running the first command followed by bash still required to get the interactivity after closing the first command).

Yeah I have tried that and few other combinations. They don't seem to work.

I'm not sure what's going on with the workdir option. Are you specifying it using that $USER variable or are you just using that as a placeholder when posting? (I would assume you should be able to use tilde paths e.g. ~/Desktop/Odin-angular, otherwise you might just need to use a fully explicit path e.g. /home/youruser/Desktop/Odin-angular)

Thanks, this makes it simpler :)

However, I found something called dbus to control the konsole. I copy pasted some example on the internet and made few tweaks of my own.

#start
ANGULAR="~/Desktop/Projects/Odin-angular"
DJANGO="~/Desktop/Projects/Odin-django"

# You can also run commands and set title:
konsole&sleep 0.1
one=$(qdbus org.kde.konsole-$! /Windows/1 newSession default $ANGULAR)
two=$(qdbus org.kde.konsole-$! /Windows/1 newSession default $ANGULAR)
three=$(qdbus org.kde.konsole-$! /Windows/1 newSession default $DJANGO)
four=$(qdbus org.kde.konsole-$! /Windows/1 newSession default $DJANGO)

qdbus org.kde.konsole-$! /Sessions/$one setTitle 1 Angular-Server
qdbus org.kde.konsole-$! /Sessions/$one runCommand 'npm start'
# qdbus org.kde.konsole-$! /Sessions/$one sendText pwd

qdbus org.kde.konsole-$! /Sessions/$two setTitle 1 Angular
# qdbus org.kde.konsole-$! /Sessions/$two sendText 'cd ~/Desktop/Projects/Odin-django'

qdbus org.kde.konsole-$! /Sessions/$three setTitle 1 Django-Server
qdbus org.kde.konsole-$! /Sessions/$three runCommand 'python3 manage.py runserver'

qdbus org.kde.konsole-$! /Sessions/$four setTitle 1 Django

#end

I dont understand whats happening but it works like a charm!
It opens four tabs in the specific order and runs the commands and keeps the server tabs interactive when ctrl + c is pressed.

Is there a better way to write the same script? :P

There is only one issue with it. KDE shows a error popup having the below message -
The D-Bus methods sendText/runCommand were just used.
There are security concerns about allowing these methods to be public.
If desired, these methods can be changed to internal use only by re-compiling Konsole.
This warning will only show once for this Konsole instance.
airdrik
Registered Member
Posts
1854
Karma
5
OS
The mention about including "&" at the end was that you should probably remove it as it may be doing something you didn't want it to do.

Dbus is a system for interacting with running processes and is often useful for writing scripts.
The interactivity is provided by the fact that each tab created in Konsole will open a Bash prompt to provide the command-line interface. Then the dbus RunCommand calls instruct Konsole to pass that string to the open Bash prompt to run, which then runs the indicated command, after which it returns to the Bash prompt.

The warning when using the dbus RunCommand call is because this is a known security hole by which a malicious process running as the user could use d-bus to hijack a running Konsole session to run various malicious commands, especially if you've logged in as root or used sudo with cached password so that the malicious process doesn't have to provide the admin credentials to do admin things like give itself unfettered access to your system. There have been recent discussions about what to do about this useful but dangerous feature.

For now it's fine to go ahead and use the d-bus interface, but just be aware that it may go away or have changes made to it in order to resolve the security hole.


airdrik, proud to be a member of KDE forums since 2008-Dec.
ganji.chetan
Registered Member
Posts
7
Karma
0
OS
Okiedokie, Thanks for your help.
I really appreciate it :)


Bookmarks



Who is online

Registered users: bancha, Bing [Bot], Evergrowing, Google [Bot], lockheed, mesutakcan, Sogou [Bot]