Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Screen (program) output to a file

People also ask

How do you save the output of a command to a file in Linux?

Method 1: Use redirection to save command output to file in Linux. You can use redirection in Linux for this purpose. With redirection operator, instead of showing the output on the screen, it goes to the provided file. The > redirects the command output to a file replacing any existing content on the file.

How do I view screen logs in Linux?

Enable Screen Logging in Linux To activate the screen logging function, just press “Ctrl-A” and “H“.

How do you detach a screen?

To detach it, type Ctrl-a Ctrl-d (most commands in screen start with Ctrl-a, this overrides the Ctrl-a command normally used when you want to jump to the start of a line).

What Screenlog 0?

You can find the screenlog. 0 file in screen 's current working directory (cwd). This is either the directory that was the cwd when you started screen , or it's a directory you subsequently changed to using the :chdir screen command.


There is a command line option for logging. The output is saved to screenlog.n file, where n is a number of the screen. From man pages of screen:

‘-L’ Tell screen to turn on automatic output logging for the windows.


You can also use Control-a + H to save loggings into screenlog.n file. One more Control-a + H to turn off.

C-a H: Begins/ends logging of the current window to the file "screenlog.n".


The following command works for Screen version 4.06.02:

screen -L -Logfile Log_file_name_of_your_choice command_to_be_executed

From the man page of Screen:

-Logfile file : By default logfile name is "screenlog.0".
                You can set new logfile name with the "-Logfile" option.

You can check the existing version of Screen using screen -version. You can download and install the latest Screen version from https://www.gnu.org/software/screen/.


The selected answer doesn't work quite well with multiple sessions and doesn't allow to specify a custom log file name.

For multiple screen sessions, this is my formula:

  1. Create a configuration file for each process:

    logfile test.log
    logfile flush 1
    log on
    logtstamp after 1
    logtstamp string "[ %t: %Y-%m-%d %c:%s ]\012"
    logtstamp on
    

    If you want to do it "on the fly", you can change logfile automatically. \012 means "new line", as using \n will print it on the log file: source.

  2. Start your command with the "-c" and "-L" flags:

    screen -c ./test.conf -dmSL 'Test' ./test.pl
    

    That's it. You will see "test.log" after the first flush:

    ...
    6 Something is happening...
    [ test.pl: 2016-06-01 13:02:53 ]
    7 Something else...
    [ test.pl: 2016-06-01 13:02:54 ]
    8 Nothing here
    [ test.pl: 2016-06-01 13:02:55 ]
    9 Something is happening...
    [ test.pl: 2016-06-01 13:02:56 ]
    10 Something else...
    [ test.pl: 2016-06-01 13:02:57 ]
    11 Nothing here
    [ test.pl: 2016-06-01 13:02:58 ]
    ...
    

I found that "-L" is still required even when "log on" is on the configuration file.

I couldn't find a list of the time format variables (like %m) used by screen. If you have a link of those formats, please post it bellow.

Extra

In case you want to do it "on the fly", you can use this script:

#!/bin/bash
if [[ $2 == "" ]]; then
    echo "Usage: $0 name command";
    exit 1;
fi
name=$1
command=$2
path="/var/log";
config="logfile ${path}/${name}.log
logfile flush 1
log on
logtstamp after 1
logtstamp string \"[ %t: %Y-%m-%d %c:%s ]\012\"
logtstamp on";
echo "$config" > /tmp/log.conf
screen -c /tmp/log.conf -dmSL "$name" $command
rm /tmp/log.conf

To use it, save it (screen.sh) and set +x permissions:

./screen.sh TEST ./test.pl

... and will execute ./test.pl and create a log file in /var/log/TEST.log


For the Mac terminal:

script -a -t 0 out.txt screen /dev/ttyUSB0 115200

Details

  • script: A built-in application to "make a typescript of terminal session"
  • -a: Append to output file
  • -t 0: Time between writing to output file is 0 seconds, so out.txt is updated for every new character
  • out.txt: Is just the output file name
  • screen /dev/ttyUSB0 115200: Command from question for connecting to an external device

You can then use tail to see that the file is updating.

tail -100 out.txt

Ctrl+A then Shift+H works for me. You can view the file screenlog.0 while the program is still running.


A different answer if you need to save the output of your whole scrollback buffer from an already actively running screen:

Ctrl-a [ g SPACE G $ >.

This will save your whole buffer to /tmp/screen-exchange