nohup
命令用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行。默认情况下,nohup
会将输出重定向到名为 nohup.out
的文件中。随着时间的推移,这个文件可能会变得非常大,因此需要进行日志切割。
以下是使用 logrotate
工具进行日志切割的方法:
确保已经安装了 logrotate
。在大多数 Linux 发行版中,它已经是预装的。如果没有,可以使用包管理器进行安装,例如在 Ubuntu/Debian 上使用 sudo apt-get install logrotate
。
创建一个名为 nohup-out.conf
的配置文件,用于指定 nohup.out
文件的日志切割规则。可以使用文本编辑器创建和编辑该文件,例如使用 nano
:
sudo nano /etc/logrotate.d/nohup-out
在 nohup-out.conf
文件中,添加以下内容:
/path/to/your/nohup.out {
daily
rotate 7
compress
missingok
notifempty
create 0640 user group
}
这里的配置表示:
daily
)rotate 7
)compress
)missingok
)notifempty
)create 0640 user group
)请根据实际情况替换 /path/to/your/nohup.out
、user
和 group
。
保存并关闭 nohup-out.conf
文件。
测试 logrotate
配置是否正确。运行以下命令:
sudo logrotate -f /etc/logrotate.d/nohup-out
如果没有错误信息,说明配置正确。
现在,logrotate
将根据指定的规则自动切割 nohup.out
日志文件。如果需要手动触发日志切割,可以运行 sudo logrotate -f /etc/logrotate.d/nohup-out
命令。