监控指定任务,结束钉钉通知

使用场景:你已经运行了一个长时间任务,临时希望在任务结束后通知,这种情况下可以循环监控指定的PID号,如果PID消失了就发送通知,逻辑简单粗暴,但胜在好用

本篇内容为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

相关推荐

  1. 监控指定任务结束通知

    2024-04-22 10:02:04       12 阅读
  2. spring boot admin服务端配置邮件通知通知

    2024-04-22 10:02:04       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-22 10:02:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-22 10:02:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-22 10:02:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-22 10:02:04       20 阅读

热门阅读

  1. 【设计模式】模板方法模式

    2024-04-22 10:02:04       12 阅读
  2. K8s ingress-controller中nginx文件上传大小的限制

    2024-04-22 10:02:04       13 阅读
  3. .NET Core中间件管道MAP的作用和使用

    2024-04-22 10:02:04       13 阅读
  4. 5G 边缘计算如何赋能工业自动化生产线?

    2024-04-22 10:02:04       16 阅读
  5. 富格林:利用正规方法提升出金收益

    2024-04-22 10:02:04       14 阅读
  6. 【php快速上手(十一)】

    2024-04-22 10:02:04       13 阅读
  7. 【Pytorch】torch.cat()函数

    2024-04-22 10:02:04       17 阅读