【问题标题】:Email before and after restart of PID - Bash script重新启动 PID 前后的电子邮件 - Bash 脚本
【发布时间】:2021-06-17 07:19:57
【问题描述】:

我创建了一个简单的脚本,如果超出阈值,它将列出前 10 个进程。列出前 10 个进程后,它将杀死前两个 PID -9,然后发送一封电子邮件作为 FYI。

电子邮件包含重启前后的 PID 列表。我将之前和之后的过程保存到一个 txt 文件中以便于跟踪。

但是,在 PID 被杀死后,“之后”的 PID 不会生成,因为之前和之后的 txt 文件同时生成。所以即使认为它杀死了 PID,它也没有显示“之后”的结果。

我错过了什么? 这是脚本的一部分:

go_send_mail (){

#after restart values

m_after=$tst_gs_dir/tst_memafter.txt
t_after=$tst_gs_dir/tst_topafter.txt

wm_after=$(echo -e "`date` \n $chk" > "$m_after")
wt_after=$(echo -e "`date` \n $top" > "$t_after")
topproc3=$(more "$t_after")
memchk3=$(more "$m_after")

echo -e "Processes
\n Memory check before restart: \n
\n $memchk2
\n \n Top processes before the restart:
\n \n $topproc2 \n
\n Processes after the restart: \n
\n $topproc3 \n
\n Memory check after restart: \n
\n $memchk3 \n \n `date`" | mailx -s "$subject" -r "$mail_from" abc@gmail.com
}

if [[ "$memtst" -le  "$threshold" ]]; then #for testing
echo "Saving previous processes"
sleep 5
#writes results to a file#
topproc=$(echo -e "`date` \n $top" > "$file")
memchk=$(echo -e "`date` \n $chk" > "$file_chk")
topproc2=$(more "$file")
memchk2=$(more "$file_chk")
#restart here
echo "Killing PIDs..."

kill -9 $pid1 $pid2
sleep 2
echo "PIDs killed."
go_send_mail

【问题讨论】:

  • 请添加一个shebang,然后将您的脚本粘贴到shellcheck.net 并尝试实施那里提出的建议。

标签: bash unix


【解决方案1】:

在 shellcheck.net 中没有发现问题。

我所做的就是为“后”进程创建一个单独的脚本并从主脚本中调用它。

cd $gs_dir

#for testing#
#cd $tst_streams_dir
#cd $tst_gs_dir

##Separate script to write after PID restart is completed##
sh ./procafter.sh
topproc3=$(more "$t_after")
memchk3=$(more "$m_after")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    相关资源
    最近更新 更多