Execute a command in a new named screen

gnu-screen

I have a script that creates a new screen and executes a command (dev webserver) in it.

screen -S webserver -dm sh launch_webserver.sh

However, when the command is done (or webserver quits), the screen is destroyed. How can I avoid that ?

Best Answer

What do you want to happen when the command is finished? The screen session ends when there are no more running commands, so if you want to keep the session around, you'll have to run another command.

If you want to keep the window around and run more commands it it, run a shell when the initial command finishes.

screen -S webserver -dm sh -c 'launch_webserver.sh; echo $?; exec bash -i'

If you want to keep the window around just to see the output, run a command that waits, such as sleep (with a near-infinite argument) or read (waits until you press Enter).

screen -S webserver -dm sh -c 'launch_webserver.sh; echo $?; read'

If you want to keep the session around but not the window, run another command. You'll have to write a screenrc file (which is a good idea anyway).

screen -c /path/to/webserver.screenrc

where /path/to/webserver.screenrc contains

sessionname webserver
screen -t webserver launch_webserver.sh
screen -t shell bash
Related Question