Sending ctrl-c to specific screen session
Asked Answered
N

1

15

I am designing a script to launch a process inside a named screen session.

as_user "screen -p 0 -S **$command** -X eval 'stuff \"wine LFS.exe /cfg=**$command**.cfg\"\015'"

So bash myscript.sh start test will create a screen named test and run the test.cfg with the software.

Now I want my script to access the specific screen session and do a CTRL+C to stop the running process so i can kill the screen session.

Something like this:

as_user "screen -p 0 -S **$command** **... kill the process with ctrl-c...**"
as_user "screen -p 0 -S **$command** -X eval 'stuff \"exit\"\015'"
Nosology answered 14/4, 2013 at 16:48 Comment(0)
L
28

I don't quite understand you but to send ctrl-c to a window in a screen session:

screen -S session_name -X at window_number stuff $'\003'
# or
screen -S session_name -X -p window_number stuff $'\003'

If you want to send something to all the windows, use # (needs to be quoted) as the window_number.

UPDATE:

Screen's stuff command also supports the use of ^X (or ^x) to represent CTRL-X so the following command can also be used to send CTRL-C.

# Here '^C' is two chars, '^' and 'C'
screen -S session_name -X at window_number stuff '^C'
Laster answered 15/4, 2013 at 1:50 Comment(8)
Could not get at to work, but this worked for me for the active window only, which might be useful for someone: screen -S session_name -X stuff $'\003'Coneflower
screen -S session_name -X at "#" stuff $'\003' with screen -S session_name -X quit works perfect for me. I am able to kill any screen session now. Thank you.Cyclostyle
Awesome, this was exactly what I was looking for! Do you have more information the $'\003' encoding? Can I assume Ctrl+A would be 001, Ctrl+B would be 002 etc? Also, is there a way to send cursor up, down, left, and right keys?Praedial
for ctrl chars see en.wikipedia.org/wiki/…Laster
Oh and now that I'm wondering, since you mentioned screen also supports ^C. How do I type a literal ^ character?Praedial
in the answer, for ^C, the ^ is the caret char. it's not the control char.Laster
to send cursor up/down/left/right, you can first get the control sequence with tput. e.g. to get cursor up you can tput cuu1. details can be found in terminfo(5).Laster
Thanks! screen -S session_name -X at window_number stuff '^C' works great on CentOS 7.Swink

© 2022 - 2024 — McMap. All rights reserved.