3

Scenario;

I have SSH'ed to a machine, opened a new screen session, and fired off a script.

Some days later I SSH back to that machine, re-attach the screen session and look at the output that has been generated, however; I can't scroll back through the output.

From what I can see, screen stores one "screens worth" of stdout output. If my script has generated 100 lines of output in 48 hours, I can't see it all, just the last 40 odd lines or so. Is there a way to make screen store all stdout from the script I leave running, so I can re-attach the screen and PgUp/PgDn through it as if it were a script running on my local machine?

Perhaps screen isn't the most optimal way to do this? Is there a better method for leaving scripts running on remote machines after log out, and being able to re-attach to that process at a later date and view all the output?

4 Answers 4

4

You can simply store the output in a file

my_command > mylogfile

If you want to see what is happening while the command is running you can use tail on a second screen:

tail -f mylogfile

You will not only be able to see the whole output but also to process it (compare, search, ...)

Edit if you don't want to use screen you can always use nohup and close the session

nohup my_command > mylogfile &
6
  • this answer might suffice, but to completely disconnect you'll want to start the job with nohup
    – bsd
    Mar 16, 2012 at 10:01
  • @bdowning @javano asked about a session using screen, hence my answer. I'll edit to clarify
    – Matteo
    Mar 16, 2012 at 10:04
  • I knew that you meant to use a redirect ">" inside a screen session ;) Although obvious I didn't try this, I thought there maybe a better way of doing it but actually, this is fine, I can myscript > output and then CTRL+Z and use bg to keep it running. Thanks!
    – Baldrick
    Mar 16, 2012 at 10:40
  • In the last paragraph he mentions not using screen. Leaving a command running with stdout redirected to a file, with more/less/tail to view data necessitates the use of nohup. I just wanted to make certain that was added. +1 from me.
    – bsd
    Mar 16, 2012 at 11:15
  • @bdowning Thanks, yes I should learn to read the whole question... :-)
    – Matteo
    Mar 16, 2012 at 12:16
4

You can also turn on output logging with parameter -L when you start your screen, via:

screen -L -S <screen_name>

This will create a log file called screenlog.0 in the directory in which you started the screen from the command above.

To view screenlog.0 use cat (not vi) SO Ref.

cat screenlog.0
1
  • Great thanks, another useful option I didn't know about!
    – Baldrick
    May 31, 2012 at 9:30
3

Screen does keep a log of past output lines; it's called the “scrollback history buffer” in the Screen documentation.

To navigate through the scrollback, press C-a ESC (copy). You can use the arrow and PgUp/PgDn keys to navigate, as well as other keys to search and copy text. Press ESC to exit scrollback/copy mode.

By default, Screen only keeps 100 lines' worth. Put a defscrollback directive in your .screenrc to change this figure.

If you want a complete log from your script, save it in a file. Run it inside Screen to be able to connect back to the parent shell and easily see if the script is still running, suspend and restart it, etc. Or fork it off with nohup. To monitor the log file while it's growing, use tail -f.

1
  • Ah! An excellent answer. This is an interesting feature I didn't know. Thanks very much! :)
    – Baldrick
    Mar 19, 2012 at 11:08
3

If you want to both see the output on screen and also log it then just pipe it to tee with the name of your log as the parameter.

You must log in to answer this question.

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