Gnu screen automation


i'd like to launch GNU screen, some shell command in it, then open new screen window(s) with different command(s) – all that automatically, from a script. is it possible?


the screen windows should not be closed after executing the commands, i just want to have the screen session preserved (and detached)

Best Answer

You can put your commands in a file, e.g., myscreenrc, like this:

screen command1
screen command2
screen command3

and then execute screen with the -c option followed by the name of that file, e.g.,

screen -c myscreenrc


in reponse to the updated question. The commands I used for testing were screen vim foo and screen vim bar, so I didn't see the screen-closing problem. The following solves the screen-closing problem, but it seems a bit of a kludge.

screen bash -c 'ls foo; exec bash -i'
screen bash -c 'ls bar; exec bash -i'

ls was just a convenient command for testing this problem.


Another approach would be to start the command from the shell's rc file rather than from screen's rc file. It requires another file for each command, though. For example, to run top in a screen window such that quitting top will return you to a bash prompt in that same window, create a file, call it runtop, that contains the following:

. ~/.bashrc

Then put this line in the file we're calling screenrc:

screen bash --rcfile ~/runtop -i

and start screen as

screen -c screenrc