Leave remote command running storing output

5,604

Solution 1

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.

Solution 2

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

Solution 3

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 &

Solution 4

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.

Share:
5,604

Related videos on Youtube

jwbensley
Author by

jwbensley

Senior network engineer / architect Programmer Hobbyist hardware hacker/tinkerer

Updated on September 18, 2022

Comments

  • jwbensley
    jwbensley over 1 year

    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?

  • bsd
    bsd about 12 years
    this answer might suffice, but to completely disconnect you'll want to start the job with nohup
  • Matteo
    Matteo about 12 years
    @bdowning @javano asked about a session using screen, hence my answer. I'll edit to clarify
  • jwbensley
    jwbensley about 12 years
    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!
  • bsd
    bsd about 12 years
    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.
  • Matteo
    Matteo about 12 years
    @bdowning Thanks, yes I should learn to read the whole question... :-)
  • Splanger
    Splanger about 12 years
    nohup - the poor mans daemonizer
  • jwbensley
    jwbensley about 12 years
    Ah! An excellent answer. This is an interesting feature I didn't know. Thanks very much! :)
  • jwbensley
    jwbensley almost 12 years
    Great thanks, another useful option I didn't know about!