2

I know you can run a program in a detached screen, but I would log into an existing screen, kill the currently running process, restart it and then detach.

Is it possible to do that with a bash script, how can I send ctrl-A ctrl-D and Ctrl-C for instance?

  • You might need to use expect, but there might be screen options that are easier to use than sending keystrokes – muru Oct 27 '16 at 14:20
1

If you want to remove a screen you can also delete the screen name in /var/run/screens/{user/ and do a screen -wipe.

But screen has a stuff and a paste command:

  • Command: paste [registers [destination]]

    (C-a ], C-a C-])
    

    Write the (concatenated) contents of the specified registers to the stdin stream of the current window. The register ‘.’ is treated as the paste buffer. If no parameter is specified the user is prompted to enter a single register. The paste buffer can be filled with the copy, history and readbuf commands. Other registers can be filled with the register, readreg and paste commands. If paste is called with a second argument, the contents of the specified registers is pasted into the named destination register rather than the window. If ‘.’ is used as the second argument, the display's paste buffer is the destination. Note, that paste uses a wide variety of resources: Usually both, a current window and a current display are required. But whenever a second argument is specified no current window is needed. When the source specification only contains registers (not the paste buffer) then there need not be a current display (terminal attached), as the registers are a global resource. The paste buffer exists once for every user.

  • Command: stuff {string}

    Stuff the string string in the input buffer of the current window. This is like the paste command, but with much less overhead. You cannot paste large buffers with the stuff command. It is most useful for key bindings. See Bindkey.


So this works:

screen -S session_name -X at window_number stuff ^C
screen -S session_name -X at window_number stuff ^X

and sends a control-c and control-x respectively and you can use it also to combine keycodes (like control a, d)

|improve this answer|||||
  • Exactly what I needed, stuff! To send a new line works with \n, is that how you are supposed to do it? – user1506145 Oct 27 '16 at 15:15
  • I expect ^m for enter – Rinzwind Oct 27 '16 at 15:16
  • I have been having so much trouble still, I need to send Ctrl+a and then H but I am having no luck.Do I need a delay in my script? Because the two parts cannot be pressed at the same time. – Zach W. Jun 26 '18 at 23:13
  • Why not use screen -S test -X log on and screen -S test -X log off? – Rinzwind Jun 27 '18 at 7:37
2

If you want to simulate keystrokes, a neat scripting language is "expect".

A sample expect script might be something like:

spawn $env(SHELL)
match_max 100000
send -- "screen\n"
expect eol
send -- "ls\n"
send -- "^Ad"
expect eof

This would be executed with:

expect ./myscript
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.