I have created multiple screen to run the same piece of code with different parameters. The way I'm doing it right now is manually attaching one screen, pass the command and arguments and then Ctrl a+d to detach that screen. Then again attaching a different screen and again passing the arguments and detaching from that screen. Is it possible to write a bash script to do this whole process automatically?


Creating multiple screen sessions is probably not the best option. Screen supports the notion of putting multiple windows within one session, which makes collections like that easier to handle. There's ^A 1, ^A 2 etc. ^A ' and ^A " to switch between the windows and ^A w to list them.

Going with one screen session, you can start a session and then the commands inside with something like:

screen -d -m -S test
screen -S test -X screen -t title somecommand someargs...
screen -S test -X screen -t othertitle somecommand otherargs

screen -d -m starts a new detached session, -S gives the session a name or refers to one by name. -X sends the rest of the command line as a command to a running session, and the screen command (within screen) opens a new window and runs a command there. -t can be used with screen to give the window a title.

Or, you could put the commands for screen in a file and then use :source to run the file (similar to .screenrc). See the manual for some examples and a reference on the commands screen supports. (There are a truckload.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.