25

I have a screen instance running, and I would need to execute some code inside the screen, and get the result out to my script.

The first part is quite easy, I just screen -S session_name -X eval 'stuff "$cmd"\015'.
(I modified a line I found in a script)

The second part, getting out the output, is trickier. How can I get the whole output, whatever it's size?

23

You could start screen with the -L option. This will cause screen to create a file screenlog.n (the n part is numerical, starting with a zero) in the current working directory.

In your case this would look something like: screen -S session_name -L -X eval 'stuff "$cmd"\015'

As long as you remember to clean up afterwards, this should match what you are after.

For last line of the log, it can easily be obtained with tail -1 screenlog.0, or the entire log can be parsed however you wish.

  • One thing, though: the screen command I gave was just to send the command in. To start the screen, I used screen -dmS name program ;-) – 1ace May 4 '11 at 15:02
17

One more nuance that may be helpful in the future: If you're in a screen session, you can interactively ask for the log file to be created. Press ctrl-a H (Control-A followed by capital H) to ask screen to start dumping whatever window you're in to a log file.

  • 4
    Don't know if it is because of a newer version but my man screen says, I have to type C-A H, without the Control for the H. Sad only, that this doesn't capture the previous log. – Matmarbon Aug 1 '12 at 10:43
  • 1
    You're right! It's C-A H to generate a log file. Please note that it's capital H (lowercase h produces just a hardcopy of the current screen). – rickumali Aug 4 '12 at 11:52
  • 2
    Please update the answer to C-A H. Future searchers may be mislead and miss the correct answer. – gc5 Feb 22 '14 at 0:57
  • Does anyone know of a way to send the command through a script? As in send control+a and then the H after a split second. – Zach W. Jun 26 '18 at 23:27

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.