9

In a Bash script, I use "screen -L" to log executed commands in color. For example:

screen -L tree

Then we read the logfile with less -R.

When this script is executed, other screens are potentially running so we don't know which screenlog.* contains our output. I can't demand the user to customize his/her .screenrc.

Is there a way to specify a log name on command line or to read specific .screenrc commands?

Peter Mortensen
  • 2,318
  • 5
  • 23
  • 24
Philippe Blayo
  • 281
  • 1
  • 2
  • 10
  • see my answer here: http://stackoverflow.com/questions/14208001/save-screen-program-output-to-a-file/37559327#37559327 – lepe Jun 01 '16 at 04:18

4 Answers4

16

I have a couple thoughts on this. First, note you can control the startup screenrc when invoking screen via the -c command line switch. Second, you can use environment variables in your .screenrc. Putting this all together, here's a shell script to do something like what you want:

#!/bin/bash

cat << EOF >/tmp/screenrc.$$
logfile /tmp/screenlog.$$
EOF

screen -c /tmp/screenrc.$$ -L
rm /tmp/screenrc.$$

echo "logfile is /tmp/screenlog.$$"

that script overrides the user screenrc and places the output in a specific file. In this case I'm using $$ to generate the file name by appending the script process name. Note that you should generally use mktemp instead to create secure temporary files but I'm lazy right now.

Also this completely replaces the user .screenrc. If you want to still read settings from that file, you should change the generated config file to something like this:

logfile /tmp/screenlog.$$
source $HOME/.screenrc
Phil Hollenback
  • 14,947
  • 4
  • 35
  • 52
4

In screen 4.06 and later, the log file can be specified directly on the command line with -Logfile <name>.

For earlier versions, the accepted answer is likely to be the clearest option.

(In screen 4.05 a log file name can confusingly be given as an optional argument to the -L option, which is a recipe for problems if someone tries to give a bare -L option as the last option: the command name gets misinterpreted as a log file name).

ncoghlan
  • 183
  • 1
  • 1
  • 7
2

Use:

tree -C > tree.log

The -C option forces color on even when the output is not to a tty.

Similarly:

ls -l --color=always > ls.log
grep --color=always foo bar > grep.log
ack --color foo > ack.log

Utilities that output color often have ways to force it on when output is sent to a pipe or redirected.

Peter Mortensen
  • 2,318
  • 5
  • 23
  • 24
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
2

Alternatively, there's a way to do it online.

Enter command mode in screen via Ctrl + A, :, and use the logfile command with the name of the file you want as argument:

logfile whatevernameyoulike.log

Source: Screen man page

Peter Mortensen
  • 2,318
  • 5
  • 23
  • 24
Juan Enciso
  • 121
  • 1