Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
sunnywx
V2EX  ›  Linux

正在运行的应用,使用 cat /dev/null > logfile 后文件空间不释放

  •  
  •   sunnywx · Apr 15, 2024 · 2721 views
    This topic created in 945 days ago, the information mentioned may be changed or developed.

    请教各位大佬,我有个 java 的应用,用的是nohup java -jar > logfile.log & 这样启动的,有时候日志文件比较大我想清理下,但是又不想停止应用,使用以下的几个方法

    方法 1
    cat /dev/null > logfile
    方法 2
    : > logfile
    方法 3
    cat "" > logfile
    
    

    执行后发现日志文件里面的内容为空了,但是日志文件的大小没有任何变化。

    大佬能告诉我是哪里出问题了么?有啥解决办法呢? 谢谢各位大佬!

    Supplement 1  ·  Apr 16, 2024

    暂时发现一个解决办法 修改启动命令为:nohup java -jar > logfile.log 2>&1 & 后就可以用那几种方式清空日志了

    guyeu
    guyeu
    Apr 15, 2024
    1
    ```shell
    truncate -s 0 /path/to/logfile
    ```

    :(
    依然搞不明白 V2 评论区咋发源码。
    sunnywx
    sunnywx
    OP
    Apr 15, 2024
    2
    @guyeu 不行,试过了,
    guyeu
    guyeu
    Apr 15, 2024
    3
    @284716337 操作系统,文件系统是什么?
    sunnywx
    sunnywx
    OP
    Apr 15, 2024
    4
    @guyeu centos 7.6 ext4 的磁盘格式
    yanqiyu
    yanqiyu
    Apr 15, 2024
    5
    是不是程序已经释放+重新打开了,要不去对应的 /proc/pid/fd/N 来 truncate 以下
    chingyat
    chingyat
    Apr 15, 2024
    6
    truncate 应该是有效的,这不过程序已经打开的文件描述符不会自动 seek 到文件开头,当程序再次输出的时候,文件的大小还是会增加。这时候文件前面的部分应该是空洞。可以通过 ls -lsh logfile 比较。
    tomychen
    tomychen
    Apr 17, 2024
    7
    touch newfile ;cp newfile /path/to/logfile
    cnleon
    cnleon
    Apr 17, 2024
    8
    你是怎么确认文件大小没有变化的? ls ? du ?
    sunnywx
    sunnywx
    OP
    Apr 18, 2024 via Android
    9
    @cnleon ls -lh 和 du 看大小没有变,还有 df -h 看磁盘释放没有
    cnleon
    cnleon
    Apr 18, 2024
    10
    @284716337 你 lsof 看看吧
    keay250
    keay250
    Apr 24, 2024
    11
    我一般是这么清空日志文件的
    `echo > logfile`
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1008 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 84ms · UTC 22:14 · PVG 06:14 · LAX 14:14 · JFK 17:14
    ♥ Do have faith in what you're doing.