0

iostat我通过将命令运行到文本文件创建了一个日志文件,并使用nohup.

#nohup iostat -xm 5 > /z/logfile.txt &

后来,在我意识到我的进程被重新启动杀死后,我创建了一个每十分钟运行一次的 cronjob,执行与上述相同的操作。我还设置了日志轮换如下:

/z/logfile.txt {
        size 20M
        rotate 0
        create 0644 root root
        missingok
        notifempty

} 

现在我已经意识到这些logfile.txt被删除了,但是iostat命令一直指向已删除的文件,如lsof -n | grep deleted命令所示。那里的磁盘空间没有被释放。

如何确保文件被旋转,然后iostat指向新创建的文件,从而释放磁盘空间?

任何想法如何正确设置它?

4

3 回答 3

1

一种解决方案是编写一个程序,该程序将从 iostat 读取,写入输出文件,并接受重新打开文件的信号。例如,如果您这样做了:iostat -xm 5 | log-daemon /z/logfile.txt其中 log-daemon 是一个简单的脚本,例如:

#!/bin/bash
echo $$ > /var/run/log-daemon
exec > $1
trap 'exec > $1' SIGHUP
read line
while test $? -le 0; do 
        echo $line
        read line
done

然后在 logrotate 配置中添加一个 postrotate 子句以向 log-daemon 发送 HUP:

postrotate
               /usr/bin/kill -HUP $(cat /var/run/log-daemon)
于 2017-05-16T02:59:55.660 回答
0

将您的 cronjob iostat 命令指向软链接不起作用吗?

ln -s /z/logfile.txt iostat_link.txt
nohup iostat -xm 5 > /z/iostat_link.txt &

我以前没有使用过 logrotate,但是我通过在运行时在后台手动更改文件来测试它:

#Make the files
touch afile1.txt
ln -s afile1.txt file.txt

#Kick off loop
for i in {1..1000};do echo "running still $i" >> file.txt;sleep 3;done &

[localhost (2017-05-15 20:30:55) IP: 26.176 ~]# cat afile1.txt
running still 7
running still 8
running still 9

#Change the file out from under the loop
mv afile1.txt afile1.txt.backup;touch afile1.txt

[localhost (2017-05-15 20:31:21) IP: 26.176 ~]# cat afile1.txt
running still 15
running still 16
running still 17
于 2017-05-16T03:33:04.530 回答
0

检查记录日志的文件系统是否已满。如果您遇到这种情况,请在最坏的情况下查找并终止进程或重新启动服务器。

于 2017-05-16T04:43:33.053 回答