微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么 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")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。