Leave remote command running storing output
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.
Related videos on Youtube
jwbensley
Senior network engineer / architect Programmer Hobbyist hardware hacker/tinkerer
Updated on September 18, 2022Comments
-
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 about 12 yearsthis answer might suffice, but to completely disconnect you'll want to start the job with
nohup
-
Matteo about 12 years@bdowning @javano asked about a session using
screen
, hence my answer. I'll edit to clarify -
jwbensley about 12 yearsI 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 usebg
to keep it running. Thanks! -
bsd about 12 yearsIn 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 about 12 years@bdowning Thanks, yes I should learn to read the whole question... :-)
-
Splanger about 12 yearsnohup - the poor mans daemonizer
-
jwbensley about 12 yearsAh! An excellent answer. This is an interesting feature I didn't know. Thanks very much! :)
-
jwbensley almost 12 yearsGreat thanks, another useful option I didn't know about!