How can I launch multiple screen sessions from a single bash script?

24,326

Solution 1

I ended up taking this question to StackOverflow, where Brian Gerard answered the question. The {0..5} loop syntax is bash (3.x+) specific. By default, my system was setup to run some other shell from /bin/sh, so I changed my sharp-bang to #!/bin/bash and my problem was solved!

Solution 2

Why do you need to open so many screen sessions? Instead, why don't you try creating multiple windows (I mean as a part of single screen terminal window) inside a single screen session. You can switch between them with ctrl-a 1 or 2 or 3 etc, depending upon how many you have created and want to view the output.

This sounds like a bad approach to running screen. In case you have not heard of multiple windows in screen, read up any tutorial on screen on the web. Screen's main design goal is window multiplexing - not just attaching and detaching.

Solution 3

Unfortunately, I can't answer Your comment because I have too little reputation, so I'll have to put it into a seperate answer.

You can create a single screen session with multiple windows using a screenrc configuration file. The screen manpage tells You everything, but here's the most important things that should solve Your problem:

The following screenrc creates a screen session with 2 windows. One window will be running bash, the other will be running python (interactively):

sessionname myscreensession
screen -t command1 0 bash
screen -t command2 1 python

The name myscreensession shows when executing screen -ls and can be used as a parameter for screen -r. The strings commandN specify the window names within the screen session. The numbers (0 and 1) specifiy which window to run the command in (You don't have to use subsequent numbers).

You can also later add another window with a new command to the running screen session, e.g. with:

screen -X screen -S myscreensession -t command3 2 python3

This would create a new window running python3 in the existing session.

Share:
24,326

Related videos on Youtube

Code4Days
Author by

Code4Days

Updated on September 18, 2022

Comments

  • Code4Days
    Code4Days over 1 year

    I've written a script (that doesn't work) that looks something like this:

    #!/bin/sh
    
    screen -dmS "somename" somecommand
    
    for i in {0..5}; do
        screen -dmS "name$i" anothercommand $i
    done
    

    For some reason, if I copy and paste this into a terminal, it creates 7 detached screen sessions as I expect. If I run it from within a script, however, I get only the first session, "somename," when I run screen -ls.

    Edit: If the same can be accomplished another way (e.g. with multiple screen windows instead of sessions), I would be open those solutions as well. Thanks!

  • Keith
    Keith almost 13 years
    Exactly, screen is really designed to have one master session managing multiple terminal sessions. No need to spawn multiple master screens.
  • Code4Days
    Code4Days almost 13 years
    Ok, good advice. I would certainly like to take advantage of the windows within a single screen session. Any idea how I would do this in an automated fashion from my script (to accomplish the same as the above script)?
  • koniiiik
    koniiiik about 11 years
    For the reference, this is the answer on SO.