Roronoa Zoro
Roronoa Zoro

Reputation: 41

Execute command on specific screen on CentOS

I use screen on CentOS to run my script. Example:

Output command screen -ls:

There is a screen on:
        session-1      (Detached)
1 Socket in /var/run/screen/S-root

And I Run:

screen -r -S "session-1" -d -m -p 0 /tmp/script1.sh
screen -r -S "session-1" -d -m -p 1 /tmp/script2.sh
screen -r -S "session-1" -d -m -p 2 /tmp/script3.sh

but it's not work. I want script1.sh run on screen:0, script1.sh run on screen:1, script1.sh run on screen:2,... with option -p <screen number>. But it's not work. Please help me. Thanks!

Upvotes: 1

Views: 388

Answers (1)

Armali
Armali

Reputation: 19395

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'

Upvotes: 1

Related Questions