39

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?

2

2 Answers 2

36

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.

1
  • 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, 2011 at 15:02
29

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
  • 5
    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, 2012 at 10:43
  • 2
    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, 2012 at 11:52
  • 3
    Please update the answer to C-A H. Future searchers may be mislead and miss the correct answer.
    – gc5
    Feb 22, 2014 at 0:57
  • 1
    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, 2018 at 23:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .