How to Execute a Command Inside a Screen Session

How to create a screen executing given command?

The problem is that using the 'exec' screen command does not start a shell. 'cd' is a shell builtin, so you need a shell for it. Also, you need a shell that remains running so that screen does not terminate.

You can use the -X option to screen to send commands to a running screen session, and the 'stuff' command to send keystrokes to the current window. Try this:

screen -dmS new_screen sh
screen -S new_screen -X stuff "cd /dir
"
screen -S new_screen -X stuff "java -version
"

Yes, you need to put the quotes on the next line in order for the commands to be executed.

How to execute a command inside a screen session

In the bash shell you can use ctrl-V to explicitly put non-printable characters into a string. So try ctrl-V ctrl-L ctrl-V ctrl-M at the end of your command just before the ".

Create a detached screen, send a command to it

The correct invocation is

screen -S "Test" -X stuff 'echo Hello\r'

how to launch screen and then execute a command inside this screen?

screen [ -options ] [ cmd [ args ] ]

example: screen -S game ls

Attach to a GNU screen and then execute commands

In your second example, you are executing the command by typing it into the console. If that is the behavior you want to emulate, you can use the stuff command to have screen paste your text into the console to execute it.

ssh <user>@<remote> -a -x -t screen -x -r my_screen -X stuff \"ruby my_script.rb^M\"

(Note the ^M was generated using CTRL-V, CTRL-M).

This won't display anything to your open terminal, but when you reconnect to the screen, you should see the output of your command (assuming the screen was at a console window at the time you sent the command, which is the risk with this approach).

Execute command on specific screen on CentOS

i have 10 window in session-1 and i want to run 10 script.

Since session-1 and its windows have already been created, we don't need options -d -m. Also, of options -r -S we need only one. To execute a program in an already existing session we need option -X exec …. So, the resulting commands would be like:

screen -r session-1 -p 0 -X exec /tmp/script1.sh

But when I tried this with screen versions 4.0, the program was executed in the current (last used) window, not in the window specified by -p. Apparently -p doesn't work with -X. What did work was:

screen -r session-1 -p 0 -X stuff /tmp/script1.sh$'\n'
screen -r session-1 -p 1 -X stuff /tmp/script2.sh$'\n'
screen -r session-1 -p 2 -X stuff /tmp/script3.sh$'\n'


Related Topics



Leave a reply



Submit