Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a screen executing given command?

Tags:

gnu-screen

i'm fairly new in *nix. Is there a way to create a screen, which will immediately execute a given command sequence (with their own arguments)? Two hours of googling yields nothing - perhaps because I can't clearly state the question.

I hope for something like

screen -dmS new_screen exec "cd /dir && java -version" 

I am using screen v4.00.03 and CentOS 5.5 (kernel ver. 2.6.18-194.26.1.el5.028stab079.2)

like image 527
Alex Abdugafarov Avatar asked Aug 13 '11 07:08

Alex Abdugafarov


1 Answers

You create a screen with a name and in detached mode:

screen -S "mylittlescreen" -d -m 

Then you send the command to be executed on your screen:

screen -r "mylittlescreen" -X stuff $'ls\n' 

The stuff command is to send keystrokes inside the screen. The $ before the string 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).

This is working for me on this screen version:

$ screen -v

Screen version 4.00.03jw4 (FAU) 2-May-06

Please see man screen for details about the commands.

like image 120
kR105 Avatar answered Sep 21 '22 06:09

kR105