6

I have a work environment on my Ubuntu laptop in which I want to use three different screens.

Eg. in terminal, I usually write

screen -S mywork
run_server_1

then, ctrl-a c to create a second screen

run_server_2

etc.

I'd like to write a script to automate setting up this environment, but how can I control multiple screens from one script?

Update : I really want to be able to do this from a shell script, not a screen config. file. Is there a way to do that?

4 Answers 4

5

Reading man pages and tutorials helps

I would say that you want to do is create a file $HOME/.screenrc.multiwin

# read in your normal screenrc
# before anything else
source $HOME/.screenrc
# now start opening windows
# it's possible to set the window title with
# the -t option
# you can also specify the window number
# to launch in
screen -t server1 5 run_server_1
screen -t server2 6 run_server_2

Then running

screen -c $HOME/.screenrc.multiwin

will do what you need

3
  • Actually, I don't think this is what I want. I really would like to do this in a shell script in which I can do other things too. (And call like any other script.) Not just in a screen "configuration" file.
    – interstar
    Commented Jul 2, 2010 at 8:32
  • 1
    @interstar: run_server_1 can be another script. The above is relatively flexible if the scripts are independent. Alternatively if there is a lot of control flow/interprocess communication what about the following: don't start screens but just start servers with their output redirected to files, then in other terminals/screens you can watch those files.
    – Unreason
    Commented Jul 7, 2010 at 15:04
  • You can always generate a config to a temporary file and give that file to screen using -c . Since shell scripts are turing complete, it is possible to do almost anything this way, but probably not very easy.
    – ptman
    Commented Oct 13, 2010 at 6:21
2

Commands can be passed from outside using screen -S sessionname -X command for instance screen -S mywork -X screen run_server_2 would create a new window (same as ctrl-a c) but that window would have run_server_2 executing in it. Unlike doing it by hand,there will not be a shell running in that window, so when run_server_2 exits, the window will be closed.

Controlling multiple screens is simply a matter of making sure they're all named with -S

1
  • I believe this answer is more universal since it doesn't involve specific mechanisms (like screen's special configuration files), but plain shell scripting.
    – dess
    Commented Jun 3, 2019 at 15:17
1

I believe tmux is much more easily scriptable than screen for this type of purpose. tmux program accepts its own commands as arguments on the command line, so for example, to launch two windows: "tmux new-session -d '/bin/bash' \; new-window -d 'top'". In the first window, it will run an interactive "bash" shell, and in the second window it will run "top".

4
  • 2
    Perhaps you could show how?
    – slm
    Commented Jul 24, 2013 at 19:01
  • Sure. the tmux program accepts its own commands as arguments on the command line, so to launch a single window you type Commented Aug 9, 2013 at 22:08
  • Sure. the tmux program accepts its own commands as arguments on the command line, so to launch two windows for example: "tmux new-session -d '/bin/bash' \; new-window -d 'top'" Commented Aug 9, 2013 at 22:18
  • If you edit your answer you can add that to it.
    – slm
    Commented Aug 9, 2013 at 22:19
0

This is what worked for me

creates new screen session 'mywork' + runs cmd 'run_server_1' in a window titled 'server_1'

screen -dmS mywork -t server_1 sh -c "run_server_1"

automates ctrl-a c to create a second screen window titled 'server_2' and runs cmd 'run_server_2'

screen -S mywork -t server_2  -X screen run_server_2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .