Ivanovich
Ivanovich

Reputation: 19

Creating screen and execute command in it without actually open that screen

Is it possible to create a screen and execute a command in it without actually open it?

What I need to perform is the following:

  1. Open a screen (screen -S screen_name)
  2. Execute command in that screen

At the moment I need to manually create the screen, then enter it, and then execute the desired command.

Is it possible to do that only via 1 bash command?

Upvotes: 1

Views: 390

Answers (1)

Srini V
Srini V

Reputation: 11375

Create a screen with in detached mode:

screen -S "scr1" -d -m

Send the command to be executed on your screen:

screen -r "scr1" -X stuff $'ls -lrt \n'

The $ before the command is to make the shell parse the \n inside the quotes, and the newline is required to execute the command (like when you press enter).

Upvotes: 2

Related Questions