0

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ivanovich
  • 9
  • 4
  • Basically I need to do: 1. cd root, then wget one malware scan sh file, then chmox +x that file and then create new screen for the scan and execute that .sh file. – Ivanovich Apr 23 '17 at 09:27
  • My point was to make ALL of this just with one single command or just one row of commands. – Ivanovich Apr 23 '17 at 09:32

1 Answers1

1

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).

Srini V
  • 11,045
  • 14
  • 66
  • 89
  • Thank you so much for this, I'd been unsuccessfully trying for a day – I think including the dollar sign and `\n` together are important for the command to actually execute. – Ali Dec 10 '18 at 15:20