为什么 mailx 发送其引用文件的先前版本而不是最新版本?

问题描述

我创建了一个简单的脚本,它会自动终止前 2 个进程。但是在重新启动之前,我将前 10 个进程列表写入一个文本文件及其内存状态 (free -h),然后它会杀死前 2 个进程。但是,当它发送电子邮件时,它不会获取最新的文件,而是最后更新的文件。每次运行脚本时都会覆盖 txt 文件

file=$aa_dir/tst_topprocess.txt #file for top processes
file_chk=$aa_dir/tst_membefore.txt #file for prevIoUs memory check
topproc=$(more "$file")
memchk=$(more "$file_chk")


 if [[ "$memtst" -le  "$threshold" ]]; then #for testing
    echo -e "`date` \n $top" > $file  #writes top processes before to a file
    echo -e "`date` \n $chk" > $file_chk #writes free -h results to a file
    #sleep 5
    #restart here
    echo "Killing PIDs..."
    #kill -9 $pid1 $pid2
    sleep 5
    echo -e  Memory check before restart: \n \n $memchk  \n \n Top processes before the restart: \n \n $topproc \n \n Processes after the restart: \n \n $top \n \n `date`" | mailx -s "$subject" -r "$mail_from" abc@gmail.com

结果是: 它确实在杀死 pid 之前保存了进程,但是当它发送电子邮件时,它没有获得最新的文件。它发送上次运行的内容

例如,如果脚本每 15 分钟通过 cronjob 运行一次,则将在晚上 11:00 运行的电子邮件正文中显示文件将是晚上 10:45。

我错过了什么?如何告诉脚本获取最新保存的文件

解决方法

根据@KamilCuk 的建议,我更改了这部分代码以解决问题

topproc=$(echo -e "date \n $top" > "$file") memchk=$(echo -e "date \n $chk" > "$file_chk") topproc2=$(更多"$file") memchk2=$(more "$file_chk")