使用场景:你已经运行了一个长时间任务,临时希望在任务结束后通知,这种情况下可以循环监控指定的PID号,如果PID消失了就发送通知,逻辑简单粗暴,但胜在好用
本篇内容为shell 调用钉钉通知后续
shell
#!/bin/bash
set -e
## 上图中 access_token字段
TOKEN=''
KEYWORD='hello' # 前文中设置的关键字
function call_webhook()
{
local msg=$1
local body=$(cat <<EOF
{
"at":{
"atMobiles":["1888888888"]
},
"text":{
"content":"{{KEYWORD}} {{MSG}}"
},
"msgtype":"text"
}
EOF
)
echo $body | sed -e "s#{{MSG}}#$msg#g" -e "s#{{KEYWORD}}#$KEYWORD#g" | curl --location --request POST "https://oapi.dingtalk.com/robot/send?access_token=$TOKEN" \
--header 'Content-Type: application/json' \
--data @-
}
target_pid=52397
while true
do
if ps -p $target_pid > /dev/null
then
echo "PID $target_pid is running $(date)"
else
echo "PID $target_pid is not running. Sending notification..."
call_webhook "PID $target_pid job run over"
exit ;
fi
sleep 60
done