14

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'"
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user2280032
  • 372
  • 2
  • 5
  • 16

1 Answers1

27

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 ^X (or ^x) to mean 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'
pynexj
  • 19,215
  • 5
  • 38
  • 56
  • 2
    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'` – Billy Moon Jul 22 '15 at 19:53
  • `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. – puchu May 29 '16 at 11:45
  • 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? – RocketNuts Jan 26 '21 at 13:44
  • 1
    for ctrl chars see https://en.wikipedia.org/wiki/Control_character#How_control_characters_map_to_keyboards – pynexj Jan 26 '21 at 13:46
  • Oh and now that I'm wondering, since you mentioned screen also supports `^C`. How do I type a literal `^` character? – RocketNuts Jan 26 '21 at 13:48
  • in the answer, for `^C`, the `^` is the *caret* char. it's not the *control* char. – pynexj Jan 26 '21 at 13:52
  • 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)](https://man7.org/linux/man-pages/man5/terminfo.5.html). – pynexj Jan 26 '21 at 13:57
  • Thanks! `screen -S session_name -X at window_number stuff '^C'` works great on CentOS 7. – Binita Bharati Dec 13 '21 at 10:19