简体   繁体   中英

How to redirect the stdout and stderr in rolling file with Unix redirection

I have java application which I am running on Unix from the command prompt. I am redirecting stdout and stderr to console.out and console.err files. The file size is increasing because a lot of information is being logged.

I want to create a rolling file, when the file size increases above a particular size, eg console1.out should get created if console.out size exceeds 500KB. Currently I am using

java MyAppName > logs/Console.out 2> logs/Console.err &

How can I do this?

Pipe the result to split like this:

java MyAppName | split -b500k - Console.log

This will create a new file every time you go over 500k. See the man-page for split for more details and options.

Or you can use rotatelogs

nohup java MyAppName 2>&1 | rotatelogs -l Console_%Y-%m-%d.log 86400 &

This will create a new file with today's date every day

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM