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

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.