I'm using gdb to debug a parallel mpi-code 'prog'. For that I use a small number of processes, say 'M' and do something like
mpiexec -n M xterm -e gdb ./prog
This pops up M xterms with each of them running one gdb process on one of the files prog.
The resulting cluttering of the screen by individual windows can be rather cumbersome.
Is there any way, using any known split-window terminal emulator (say, terminator), such as to have the M gdb processes starting up in only one window, however split into M parts from start?
I faced a similar problem and have found tmpi which does exactly what you want: launch mpi debugging processes in M tmux terminal windows.
Clone the repository:
git clone https://github.com/Azrael3000/tmpi.git
then install with:
sudo ./tmpi/install.sh
which places the tmpi executable in /usr/local/bin
The tmpi executable and tmux must be in the path on all your servers.
Run a job with:
tmpi M gdb my_executable
where M is the number of processes that you want.
What you want is called a 'terminal multiplexer'; look into screen or tmux
EDIT: this is probably what you want; issue the following commands in your shell
tmux new-session -d bash # start a bash shell
tmux split-window -v python # start a python shell below it
tmux attach-session -d # enter the tmux session
Related
I am trying to open multiple tabs and execute a series of commands in each tab. Lets say I open 3 tabs tab1, tab2, tab3. Then in each tab I would like to execute following:
ssh user#address (PublicKey Authentication is setup and
hence no need to enter password)
Launch python scripts (python some.py)
Hold the tab open after executing the commands to see the outputs.
I went through some threads and have a rough outline for Bash script.
#!/bin/bash
echo "Script running"
gnome-terminal -e "bash -c \"ssh user#address; uname -a; exec bash\""
When I run the above script, a new terminal opens and I can see that I have ssh-ed into the target address but the other command uname -a didnot execute.
I would like to build upon this to implement the following:
Open multiple tabs and run commands. Ex : gnome-terminal --tab -e
"bash -c \"ssh user#address; python file1.py; exec bash\"" -tab -e
"bash -c \"ssh user#address; python file2.py; exec bash\""
Wait for one of the python file to start executing before opening
another tab and repeating the process for another python file.
Also is there a better way to implement the same task ?
The above code snippet was from this thread.
You should consider using screen or tmux or a similar terminal multiplexer for this.
Example usage:
screen -d -m bash -c 'ls; bash'
to initiate a screen session in which ls was executed and then a shell started, and then
screen -X screen bash -c 'date; bash'
to create a new window in the existing screen session, run date therein and then start a shell in that window.
Mind that the programs are run without you seeing their output right away on your controlling terminal. You can then attach to the screen session using
screen -x
Which attach you to the running session and will show you one of the screen windows (the virtual terminals of your two running programs). Typing Ctrl-A n will switch through the windows, Ctrl-A d will detach you again, leaving the programs running, so you can attach later with screen -x.
You can attach from several locations (e. g. from two different Gnome-terminals) to the same running windows. Both will then show the same contents.
Another advantage of using screen is that you can log out and the programs keep running. If you later login again, you can still attach to the running sessions.
Only a direct attack like a reboot, a kill-signal or an interaction (like pressing Ctrl-C while being attached) will terminate your programs then.
I'm using multimonitor workstation and need emacs running as daemon to assure correct working with multiple frames, for example to avoid ending emacs session after closing a frame.
When I run emacs as daemon I face problem with visuals that usually displays in GUI. Icons and connectors for the dirtree mode and same things for the sr-speedbar mode are replaced with ascii pseudo-graphics as I run emacs under console.
How can I enable GUI visuals even for daemonized emacs?
running emacs as daemon:
emacs --daemon=foo
spawning emacs frames:
emacsclient -c -s foo
emacsclient -c -s foo
emacsclient -c -s foo
All commands are run from GUI where DISPLAY variable is set
$ matlab -nodesktop -nojvm &
How would I execute matlab commands on the session that was just created?
In other words, I want to have a matlab session running in the background, and execute matlab commands and/or scripts from an arbitrary terminal at any given time without having to create a new session.
I would suggest a similar solution as carandraug did, only I prefer tmux as the multiplexer. It may be a bit tricky getting the commands passed in correctly so create a shell-script that handles the details.
Let's say you've started matlab in a terminal like this:
tmux new -s matlab "matlab -nodesktop -nojvm"
Now a tmux session called matlab is running matlab with no gui.
Create this shell-script:
mx
#!/bin/bash
if [[ $# -eq 0 ]]; then
while read; do
tmux send-keys -t matlab "$REPLY"$'\n'
done
else
tmux send-keys -t matlab "$#"$'\n'
fi
In a different terminal you can now run quoted matlab commands:
mx "A = reshape(1:9, 3, 3)"
Or even pass commands in through a pipe:
for mat in A B C; do echo "$mat = reshape(1:9, 3, 3)"; done | mx
A possibility is to start a screen session, then start matlab on it, and detach from it. Anytime you want to use it, just fire up a terminal and reattach that screen session.
Basically start screen (just type screen at a terminal), and start your matlab session. Then detach from the session (Ctrl+A followed by pressing D) and you'll be back to your terminal. You can close the window no problem, any process that started on screen will keep on running. Whenever you want to get it again (it's called reattach the session), just use screen -r. Take a look at the man page for all the other options.
Note that a screen session can have any number of windows and you can also have multiple screen session at the same time. Take a good luck at some tutorials online, it's an extremely useful tool, specially but not only, if you connect a lot to other systems that may have need to run long jobs.
We have an attended-upgrade script which launches apt-get update && apt-get upgrade simultaneously on all our administered systems. Ideally, we'd want to launch them all inside a screen session. When I do it like this:
File: upgrade.sh
for host in $ALLHOSTS
do
some_commands_which_take_considerable_time
screen -X screen sh -c "ssh $host \"apt-get update && apt-get upgrade\""
done
$ screen ./upgrade.sh
, it works, but as there are new windows arriving on the session, they are automatically being switched to. Instead, I'd rather have a version where the active window is fixed unless contained process quits or I switch manually using ^A n.
Bonus points if there is a possibility to preserve windows with exited processes, but keeping them separate from windows with active processes.
You can do this with tmux. For example:
# Start a session named "apt-get" and leave it running in the background.
tmux session-new -d -s apt-get
# Preserve windows with inactive processes.
tmux set-option -t apt-get set-remain-on-exit on
# Start a new window without switching to it. You can also do this from
# within a running tmux session, not just outside of it.
tmux new-window -d -t apt-get -n upgrade-$host \
"ssh $host 'apt-get update && apt-get upgrade'"
Note that you can have multiple windows with the same name, or modify the argument to the -n flag for unique names. The tmux application doesn't care.
As an example, you could name each window "upgrade," but that would make it difficult to identify your SSH sessions at a glance. Instead, this example appends the value of the host variable (populated by your for-loop) to each window name. This makes it easier to navigate, or to programmatically close windows you are no longer interested in. This is especially valuable when you have a lot of unclosed windows displaying terminated processes.
Overall, the syntax is a little cleaner and more intuitive than GNU screen's for this sort of task, but your mileage may vary.
W/r/t preserving windows after a subprocess exits, one possibility is to invoke the zombie command in your screen config file(s), which requires that you specify two keyboard characters that kill or resurrect the window, respectively. E.g.:
zombie KR
Then, K would kill a window whose subprocess has terminated, while R would attempt to relaunch the subprocess in the same window. Note that, in a zombie window, those keys are captured at the top level (i.e., do not precede them with your normal screen control character prefix sequence).
In order to prevent automatic switching to a newly created window, try altering your invocation of screen to something like the following:
screen -X eval 'screen sh -c "ssh $host \"apt-get update && apt-get upgrade\""' 'other'
Thanks to CodeGnome, I'll probably go with tmux as I believe there is no way to do it with screen (that's unfortunate, really!).
To give a better sketch of how it can be used:
#!/bin/sh
tmux new-session -d -s active
tmux new-session -d -s inactive
tmux set-option -t active set-remain-on-exit on
for host in $ALLHOSTS
do
tmux new-window -d -t active: -n upgrade-$host "
./do_upgrade_stuff.sh $host
tmux move-window -s active:upgrade-$host -t inactive:
"
done
my bash script reads in a few variables and then runs airodump on my home network. I would like to keep the window with airodump running and open some new terminals to run other commands for network discovery while the airodump screen is open (so i can see the results).
right now what i have looks like this (edited for brevity):
#!/bin/bash
read -p "Enter the channel: $channel"
airomon-ng start wlan0 $channel,$channel
airodump-ng -c $channel,$channel mon0 &&
terminator -e netstat -ax &&
terminator -e nmap 192.168.1.1
the first command that uses the whole terminal (airodump) starts up fine and i can see the network, but then it just stays on that screen. If i ctrl+c then it goes back to prompt but i can see the error : Usage: terminator [options] error no such option
i want the netstat and nmap commands to appear and stay in their own terminal windows, how can i do this?
The terminal window is generated by a separate program from the command running inside. Try one of these variations:
xterm -e airomon-start wlan0 "$channel","$channel" &
gnome-terminal -x airomon-start wlan0 "$channel","$channel" &
konsole -e airomon-start wlan0 "$channel","$channel" &
Pick the command that invokes the terminal program you like. You'll have to do this for every command that you want run in its own window. Also, you need to use a single & at the end of each such command line -- not a double && -- those do totally different things. And the last line of your script should be just
wait
that makes it not exit out from under all the terminals, possibly causing them all to die.
Obligatory tangential shell-scripting nitpick: ALWAYS put shell variable uses inside double quotes, unless you know for a fact that you need word-splitting to happen on a particular use.
If the script is running in the foreground in the terminal, then it will pause while an interactive command is using the terminal. You could change the script to run airodump in a new terminal as well, or you could run the background commands before you start airodump (maybe after sleeping, if you're concerned they won't work right if run first?)