GNU Screen - create screen in background run command from shell or script

jahil picture jahil · Jul 15, 2012 · Viewed 19.2k times · Source

I am trying to create a screen session/window from the shell and launch a command in it. How would I do that?

Answer

Thor picture Thor · Jul 15, 2012

If you want to launch and connect to screen:

screen CMD

If you want to launch and not connect to screen:

screen -dm CMD

Works with sessions too:

screen -Sdm NewDetachedSessionName CMD

You can send keypresses to CMD with stuff:

screen -S NewDetachedSessionName -X stuff "keypresses"

To send a new-line, include \n or ^M or $'\n' with the keypresses.