Throw a command into a detached screen session

I recently needed to script something that involved making some changes to a process running inside a screen.

screen lets you run programs in the background and detach them. You can close your terminal and the program keeps running. You can then reattach to it later.

Screen supports a wide range of commands, one of them is called stuff. When you give the stuff command to a running screen session, like this:

screen -S sessionName -X stuff "ls^M"

The command string ls is typed on that virtual terminal window, followed by ^M (Control-M), which represent a carriage return character, meaning Enter key in most cases.

In my use case, I wanted to stop a process running in a screen, then make a backup outside the screen, of some files used by the process, and finally start the process again, inside the screen.

.