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?


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.

  • 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 5:08
  • You can drop the path parts from the above and it'll use the current directory. – EightBitTony Apr 30 '16 at 5: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 5: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 5:34
  • So you mean your method probably the only way? I just saw this unix.stackexchange.com/a/257859/31374 ? However I can't reproduce the result, don't know why – user15964 Apr 30 '16 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.