{"id":3672,"date":"2022-08-12T11:32:09","date_gmt":"2022-08-12T03:32:09","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=3672"},"modified":"2022-08-12T11:32:09","modified_gmt":"2022-08-12T03:32:09","slug":"%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%8a%b6%e6%80%81","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=3672","title":{"rendered":"\u670d\u52a1\u5668\u72b6\u6001"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>crontab -e\n\n*\/1 * * * * python3 \/root\/shell\/test.py &amp;&amp; echo \"runing succeed!\" >> \/root\/shell\/test.log 2>&amp;1\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\n# -*- coding: utf-8 -*-\nimport psutil   # python\u83b7\u53d6\u7cfb\u7edf\u4fe1\u606f\u6a21\u5757\uff0c\u9700\u8981\u989d\u5916\u5b89\u88c5\nimport smtplib  # \u53d1\u9001\u90ae\u4ef6\nfrom email.mime.text import MIMEText  # \u6784\u9020\u7eaf\u6587\u672c\u90ae\u4ef6\nfrom email.utils import formataddr # \u683c\u5f0f\u5316\u90ae\u4ef6\u5730\u5740\n \ncpuUsed=psutil.cpu_percent(interval=1)\ndiskUsed=psutil.disk_usage('\/').percent\n \ndef structural_mail(text, recipient):\n    msg = MIMEText(text, 'plain', 'utf-8')\n    msg&#91;'From'] = formataddr(&#91;\"zzz\", '101xx@qq.com'])  # \u53d1\u4ef6\u4eba\n    msg&#91;'To'] = formataddr(&#91;recipient, recipient])  # recipient\u6536\u4ef6\u4eba\n    msg&#91;'Subject'] = \"\u76d1\u63a7\u62a5\u8b66\"  # \u4e3b\u9898\n    return msg\n \n \ndef send_mail(text, recipient):\n    from_addr = '101xx@qq.com'\n    password = 'pass'\n    smtp_server = 'smtp.qq.com'\n    smtp_port = 465\n    to_addr = &#91;]  # \u53ef\u4ee5\u4e00\u6b21\u53d1\u7ed9\u591a\u4e2a\u4eba,\u56e0\u6b64\u4f20\u5165\u4e00\u4e2a\u5217\u8868\n    to_addr.append(recipient)\n \n    msg = structural_mail(text, recipient)\n \n    server = smtplib.SMTP_SSL(smtp_server, smtp_port)\n    server.login(from_addr, password) \n    server.sendmail(from_addr, to_addr, msg.as_string())\n \n \ndef check():\n    if cpuUsed >= 80 or diskUsed >= 85:\n       send_mail('CPU\u4f7f\u7528\u7387\uff1a{}%\uff0c\u78c1\u76d8\u4f7f\u7528\u7387\uff1a{}%'.format(cpuUsed, diskUsed),'xxx@qq.com')\n \nif __name__ == '__main__':\n    check()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3672"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3672"}],"version-history":[{"count":4,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3672\/revisions"}],"predecessor-version":[{"id":3676,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3672\/revisions\/3676"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}