在Debian系统中,使用nohup命令运行的程序通常会将输出重定向到一个名为nohup.out的文件中。为了防止这个文件变得过大,我们可以使用日志轮转工具logrotate来管理这些日志文件。
以下是如何为nohup.out设置日志轮转的步骤:
logrotate已经安装在你的系统上。如果没有安装,可以使用以下命令安装:sudo apt-get update
sudo apt-get install logrotate
logrotate配置文件,例如/etc/logrotate.d/myapp,用于指定日志文件的轮转策略。在这个文件中,添加以下内容:/path/to/your/nohup.out {
# 每天轮转一次日志文件
daily
# 保留7天的日志文件
rotate 7
# 对旧日志文件进行压缩
compress
# 只允许root用户读取日志文件
missingok
notifempty
create 640 root adm
}
请根据你的需求修改/path/to/your/nohup.out为实际的日志文件路径。
logrotate定时任务已启用。编辑/etc/cron.daily/logrotate文件,确保其中包含以下内容:#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
logrotate服务以应用更改:sudo systemctl restart logrotate
现在,nohup.out日志文件将根据配置的策略进行轮转。你可以根据需要调整logrotate配置文件中的选项以满足你的需求。