Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a screen instance running (a lot more actually, but only one that matters), 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'm no screen expert, I just modified a line I found in a script, but I understand it... Not sure there's no better way to do it, though)

The second part, getting out the output, is trickier. For my current need, I only need the last line, but it'd be better if I can get the whole output, whatever it's size (for future uses).

share|improve this question
    
    
Thanks :) The second link was explained why the first command has to be stuff, which was what I had trouble understanding. –  1ace May 5 '11 at 13:44

2 Answers 2

up vote 12 down vote accepted

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.

share|improve this answer
    
That would do the trick, thanks :) –  1ace May 4 '11 at 14:59
    
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

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. Use the C-A H (Control-A then capital H) keystroke to ask screen to start dumping whatever window you're in to a log file.

share|improve this answer
3  
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
    
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
1  
Please update the answer to C-A H. Future searchers may be mislead and miss the correct answer. –  nsl Feb 22 at 0:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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