3

I want to run some long calculation jobs in the background. I choose screen. However I found screen didn't accpet redirection. for example

screen -dmS name ls>ls.dat

won't generate ls.dat.

Fortunately, screen -L will output screen's log to a file. However, what it does is to append to previous log file, even if I pkill screen and start a fresh new screen.

Is there a way to force it to overwrite the previous log file when starting a new screen?

EightBitTony
  • 21,373
user15964
  • 713

1 Answers1

3

I don't believe you can force screen to overwrite the log. It logs to screenlog.%n by default, where %n is the screen window number (so each window has it's own log). If that file exists, it appends to it.

However, you can tell screen to use a different filename, including a timestamp, so you'll get a new log file each time, but you'll then need to manage the old logs.

In .screenrc you can put the following line,

logfile /path/to/log/screenlog-%n-%Y%m%d-%c:%s

to create log files that include the window number (%n) and the year, month, date, and time.

Alternatively, you could create a bash alias that deletes the log file before running screen, for example,

alias screen='rm /path/to/log; screen'

If you want to affect screen log files in the current directory, just remove /path/to/log/ from the commands above.

Lastly, depending on what you're trying to achieve, the Linux tool script might be more useful than just logging in screen. man script for more information.

EightBitTony
  • 21,373
  • Hi, EightBitTony. Thank you for answering. But generally, we start screen in different folder(corresponding to different job), the logfiles are then in different folder. – user15964 Apr 30 '16 at 05:08
  • You can drop the path parts from the above and it'll use the current directory. – EightBitTony Apr 30 '16 at 05:09
  • This works! Thank you very much. But do you know why redirection is not working in screen command? for example, screen -dmS xxx ls>ls.dat – user15964 Apr 30 '16 at 05:27
  • screen doesn't send output to stdout in that way, so you can't redirect screen's output, that's why it has a logging option. – EightBitTony Apr 30 '16 at 05:34
  • So you mean your method probably the only way? I just saw this http://unix.stackexchange.com/a/257859/31374 ? However I can't reproduce the result, don't know why – user15964 Apr 30 '16 at 05:44
  • That question runs a single command within screen and sends the output from that command to a file. That's not what your question asked. If you have another query, or you're trying to solve a different issue, mark this as accepted and ask that in a new question, or, edit the question to reflect what you're actually trying to do. – EightBitTony Apr 30 '16 at 05:46
  • I updated my post. I think these two questions closely related. So I make it one. And I guess you know both answers : ) – user15964 Apr 30 '16 at 06:00
  • There is already a question on the site which discusses how to redirect within screen, you pointed to it - so this will just get closed as a duplicate. The problem you have is solved by one or the other, but they are not answers to the same question. – EightBitTony Apr 30 '16 at 06:03
  • Ok, I'll accept yours. Thank you very much :) – user15964 Apr 30 '16 at 06:08