How can I launch multiple xterm windows and run a command on each, leaving each window open afterward?
Asked Answered
E

4

7

I'm lazy, and I prefer that computers do my work for me. I ssh into several machines on a daily basis, so I created a simple script that launches some xterm windows and places them in positions I want (as you can see, I'm using bash):

#!/bin/bash
xterm -geometry 80x27+1930+0 &
xterm -geometry 80x27+2753+0 &
xterm -geometry 80x27+1930+626 &
xterm -geometry 80x27+2753+626 &

However, the next thing I do is go to the first window and type in

ssh server_a

then in the second

ssh server_b

and so on. What I'd like to do is have my script do the ssh commands in each xterm window, and then leave the windows open for me to do my work. I've seen the -e option for xterm, but the window closes after I execute my command. Is there a way to do this?

I apologize if this is a duplicate question. I've searched around and haven't had any luck with this. Many thanks!

Entelechy answered 15/12, 2014 at 17:14 Comment(1)
You want the windows to stick around after you exit from the ssh session? Or the windows are exiting immediately because you are running a specific command via ssh and you want the ssh session to stay open (and thus the window)?Hawking
R
7

I'd love to see a more elegant answer, but what I came up with does work:

xterm -e bash -c 'echo foo; exec bash'

Replace echo foo with the command of your choice, and you're good to go.

Rationality answered 15/12, 2014 at 17:25 Comment(1)
Note that (at least some versions of) xterm also have a -hold option that prevents the window going away immediately after the command terminates, so you can review final output, if that is what was desired...Royster
G
2

This answer gives one of the best answers I've seen so far to do this. Use the bash --init-file flag either in the shebang or when executing the terminal:

#!/bin/bash --init-file
commands to run

... and execute it as:

xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang

My only real complaint with the exec option is if the command executed prior to exec bash is long running and the user interrupts it (^C), it doesn't run the shell. With the --init-file option the shell continues running.

Another option is cmdtool from the OpenWin project:

/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash

... where cmdtool injects the commands passed with -I to the slave process as though it was typed by the user. This has the effect of leaving the executed commands in the shell history.

Goodwife answered 23/1, 2015 at 17:16 Comment(0)
F
0

Another option is to use gnome terminator. This creates and positions terminals interactively, and you can set up each terminal to run commands within terminator preferences.

Also does lots of extra tricks using keybindings for things like move, rotate, maximise/minimise of terminals within the containing terminator window

See: https://superuser.com/a/610048

Feel answered 8/10, 2015 at 20:13 Comment(1)
Not sure why they down voted you because this is the correct answer for that kind of work.Eugenaeugene
P
0

"ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection"

https://github.com/duncs/clusterssh/wiki

$ cssh server_a server_b
$ command
Panda answered 2/4, 2019 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.