在 CentOS 系统中,nohup
命令用于在后台运行程序,即使关闭终端也不会中断
使用 split
命令分割输出文件:
在运行程序时,可以使用 split
命令将输出文件分割成多个较小的文件。例如,以下命令将输出文件分割成每个大小为 100MB 的文件:
nohup your_command > output.log.20210901 &
split -b 100M output.log.20210901 output.log.part.
这将在后台运行 your_command
,并将输出重定向到 output.log.20210901
。然后,split
命令会将 output.log.20210901
分割成多个大小为 100MB 的文件,文件名以 output.log.part.
开头。
使用 logrotate
工具管理日志文件:
logrotate
是一个用于管理日志文件的工具,可以自动压缩、删除和分割日志文件。要使用 logrotate
管理 nohup
命令的输出文件,需要创建一个配置文件,例如 /etc/logrotate.d/your_command
,并添加以下内容:
/path/to/output.log {
size 100M
rotate 5
compress
missingok
notifempty
create 640 user group
}
这将限制 output.log
文件的大小为 100MB,并保留最近的 5 个旋转文件。旋转的文件将被压缩,如果文件不存在则不会报错,如果文件为空则不进行旋转。同时,创建新的日志文件时,将设置文件权限为 640,属主为用户(user),属组为组(group)。
然后,在运行 nohup
命令时,使用 logrotate
的 -f
选项强制执行配置:
nohup your_command > /path/to/output.log 2>&1 &
logrotate -f /etc/logrotate.d/your_command
这将在后台运行 your_command
,并将输出重定向到 /path/to/output.log
。logrotate
将根据配置文件管理日志文件。
请注意,这些方法仅限制输出文件的大小,而不是实时监控输出文件大小。如果需要实时监控输出文件大小,可以考虑使用第三方工具,如 logwatch
或 logcheck
。