{"id":249438,"date":"2022-08-15T10:54:32","date_gmt":"2022-08-15T02:54:32","guid":{"rendered":"https:\/\/gulass.cn\/?p=249438"},"modified":"2022-08-09T14:53:27","modified_gmt":"2022-08-09T06:53:27","slug":"automation-in-linux","status":"publish","type":"post","link":"https:\/\/gulass.cn\/automation-in-linux.html","title":{"rendered":"\u5b66\u4f1a\u8fd9\u4e9blinux\u7684\u201c\u81ea\u52a8\u5316\u201d\u8f7b\u677e\u641e\u5b9a\u4efb\u52a1"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nlinux\u7cfb\u7edf\u7684web\u7f51\u7ad9\u5728\u8fd0\u8425\u72b6\u6001\u65f6\uff0c\u6211\u4eec\u5e38\u9700\u8981\u5bf9\u7f51\u7ad9\u8fdb\u884c\u7ef4\u62a4\uff0c\u4f8b\u5982\u67e5\u770b\u8d44\u6e90\u5269\u4f59\u5e76\u505a\u51fa\u54cd\u5e94\u3001\u65e5\u5fd7\u5206\u5272\u3001\u6570\u636e\u6574\u7406\uff0c\u5728\u7279\u5b9a\u72b6\u6001\u6267\u884c\u7279\u5b9a\u4efb\u52a1\u7b49\u7b49\uff0c\u8fd9\u4e9b\u90fd\u4f1a\u9700\u8981linux\u80fd\u5b9e\u73b0\u81ea\u52a8\u6267\u884c\u67d0\u4e9b\u4efb\u4efb\u52a1\u3002\u672c\u7bc7\u535a\u6587\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u5e38\u89c1\u7684linux\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

linux\u7684\u201c\u81ea\u52a8\u5316\u201d<\/strong><\/div>\n

\u5b9e\u73b0\u201c\u81ea\u52a8\u5316\u201d\u6709\u5982\u4e0b\u597d\u5904\uff1a<\/p>\n

  • \u8282\u7701\u4eba\u529b\uff0c\u4e00\u4e2a\u811a\u672c\u5c31\u591f\u4e86\u3002<\/li>\n
  • \u5728\u591c\u665a\u81ea\u52a8\u6267\u884c\u53ef\u4ee5\u907f\u5f00\u7f51\u7ad9\u6d41\u91cf\u9ad8\u5cf0\u671f\uff0c\u4e0d\u5f71\u54cd\u7f51\u7ad9\u767d\u5929\u7684\u6548\u7387\u3002<\/li>\n
  • \u51c6\u786e\uff0c\u8bbe\u7f6e\u5b8c\u5584\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u4f1a\u51fa\u5dee\u9519\u3002<\/li>\n
  • \u5f53\u7136\u6700\u91cd\u8981\u7684\u8fd8\u662f\u7701\u5fc3\u4e86\uff0c\u4e0d\u7528\u9891\u7e41\u7684\u6572\u67d0\u4e9b\u547d\u4ee4\u4e86\u3002<\/li>\n
    \u5f00\u673a\u542f\u52a8<\/strong><\/div>\n

    \u5f00\u673a\u542f\u52a8\u5e94\u8be5\u662f\u6211\u4eec\u5f88\u7ecf\u5e38\u7684\u9700\u6c42\u4e86\uff0c\u6211\u4eec\u5e38\u9700\u8981\u5728\u5f00\u673a\u65f6\u5c31\u81ea\u52a8\u6267\u884c\u67d0\u4e9b\u547d\u4ee4\u6765\u5f00\u542f\u670d\u52a1\uff0c\u8fdb\u7a0b\u7b49\uff0c\u6709\u4e86\u5b83\u6211\u4eec\u4e0d\u5fc5\u518d\u5728\u6bcf\u6b21\u5f00\u673a\u65f6\u8f93\u5165\u540c\u4e00\u5806\u547d\u4ee4\u3002<\/p>\n

    chkconfig\u547d\u4ee4<\/strong><\/span><\/div>\n

    \u4f7f\u7528chkconfig\u547d\u4ee4\u53ef\u4ee5\u5728\u8bbe\u7f6e\u5728\u4e0d\u540c\u542f\u52a8\u7ea7\u522b\u4e0b\u542f\u52a8\u7279\u5b9a\u7684\u670d\u52a1\u6216\u662f\u7a0b\u5e8f\u3002<\/p>\n

    \u5148\u8bf4\u4e00\u4e0blinux\u7684\u8fd0\u884c\u7ea7\u522b\uff1a<\/p>\n

  • \u7b49\u7ea70\u8868\u793a\uff1a\u8868\u793a\u5173\u673a<\/li>\n
  • \u7b49\u7ea71\u8868\u793a\uff1a\u5355\u7528\u6237\u6a21\u5f0f<\/li>\n
  • \u7b49\u7ea72\u8868\u793a\uff1a\u65e0\u7f51\u7edc\u8fde\u63a5\u7684\u591a\u7528\u6237\u547d\u4ee4\u884c\u6a21\u5f0f<\/li>\n
  • \u7b49\u7ea73\u8868\u793a\uff1a\u6709\u7f51\u7edc\u8fde\u63a5\u7684\u591a\u7528\u6237\u547d\u4ee4\u884c\u6a21\u5f0f<\/li>\n
  • \u7b49\u7ea74\u8868\u793a\uff1a\u4e0d\u53ef\u7528<\/li>\n
  • \u7b49\u7ea75\u8868\u793a\uff1a\u5e26\u56fe\u5f62\u754c\u9762\u7684\u591a\u7528\u6237\u6a21\u5f0f<\/li>\n
  • \u7b49\u7ea76\u8868\u793a\uff1a\u91cd\u65b0\u542f\u52a8<\/li>\n

    chkconfig\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n

    chkconfig --list \/\/\u547d\u4ee4\u67e5\u770b\u5df2\u8bbe\u7f6e\u7684\u5f00\u542f\u81ea\u542f\u52a8\u5217\u8868\u3002\r\nxxxd 0:off 1:off 2:on ... 6:off \/\/list\u7684\u7ed3\u679c\uff0c\u8868\u793a\u5728xxxd\u670d\u52a1\u5728\u542f\u52a8\u7ea7\u522b\u4e3a2 3 4 5 \u7684\u60c5\u51b5\u4e0b\u4f1a\u81ea\u52a8\u542f\u52a8\u3002 \r\nchkconfig --add xxxd\/\/\u5411\u4efb\u52a1\u5217\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2axxxd\u670d\u52a1\r\nchkconfig [--level 1\/2\/..\/6] xxxd on\/off\/\/\u8bbe\u7f6exxxd\u7528\u670d\u52a1\u5728n\u72b6\u6001\u4e3a\u5f00\/\u5173\uff0c[]\u5185\u7701\u7565\u5219\u57282345\u7ea7\u522b\u5f00\u542f\r\nchkconfig --del xxxd \/\/\u5c06\u4efb\u52a1\u5217\u8868\u4e2d\u7684xxxd\u670d\u52a1\u5220\u9664<\/pre>\n
    rc.d\u6587\u4ef6\u7684\u7f16\u8f91<\/strong><\/span><\/div>\n

    \u4e5f\u53ef\u4ee5\u76f4\u63a5\u7f16\u8f91\/etc\/rc.d\/\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u6765\u5b9e\u73b0\u5f00\u673a\u81ea\u542f\u52a8\u3002\u6b64\u76ee\u5f55\u4e0b\u6709\u5f88\u591a\u6587\u4ef6\uff0crcn.d\u662f\u5728\u542f\u52a8\u72b6\u6001\u4e3an\u7684\u60c5\u51b5\u4e0b\u7684\u542f\u52a8\u6587\u4ef6\u5939\uff0crc\u3001rc.sysinit\u3001init.d\u90fd\u662f\u7cfb\u7edf\u7684\u6a21\u5757\u6216\u7cfb\u7edf\u8bbe\u7f6e\u7684\u81ea\u542f\u6587\u4ef6[\u5939]\u3002<\/p>\n

    \u6211\u4eec\u7528vim rc.local \u7f16\u8f91 rc.local\u6587\u4ef6\uff0c\u6765\u5b9a\u5236\u81ea\u5df1\u7684\u81ea\u542f\u8ba1\u5212\u3002\u547d\u4ee4\u5341\u5206\u7b80\u5355\uff0c\u5c31\u50cf\u5e73\u5e38\u5728\u64cd\u4f5c\u4e00\u6837\u3002\u5982\/usr\/local\/apache\/bin\/apachectl start\u8868\u793a\u5f00\u673a\u81ea\u542f\u52a8apache\u670d\u52a1\u5668\u3002<\/p>\n

    at\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1<\/strong><\/div>\n

    at\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u529f\u80fd\u7b80\u5355\u7684\u5b9a\u65f6\u4efb\u52a1\u7a0b\u5e8f\uff0c\u5b83\u53ea\u80fd\u8fdb\u884c\u4e00\u6b21\u6027\u7684\u5b9a\u65f6\u4efb\u52a1\uff0c\u5176\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n

    #at time\u3000\u3000\u3000\u3000\u3000\u3000\/\/at\u52a0\u65f6\u95f4\u542f\u52a8at\u547d\u4ee4\r\nat>operation\u3000\u3000\u3000\u3000\/\/\u8f93\u5165\u8981\u6267\u884c\u7684\u64cd\u4f5c\r\nat>Ctrl+D\u3000\u3000\u3000\u3000\u3000\u3000\/\/\u6309Ctrl+D\u9000\u51fa\u547d\u4ee4\u7f16\u8f91<\/pre>\n

    \u5176time\u7684\u5e38\u89c1\u5f62\u5f0f\u5982\u4e0b<\/p>\n

    at H:m tomorrow     \/\/\u7b2c\u4e8c\u5929\u7684H\u70b9m\u5206\r\nat now + n minutes\/hours\/days\/weeks  \/\/\u5728n\u5206\/\u65f6\/\u5929\/\u5468\u540e\r\nat midnight         \/\/\u5728\u5348\u591c=-=\r\nat H:m pm\/am        \/\/\u5728\u5f53\u5929\u4e0a\u5348\/\u4e0b\u5348\u7684H\u70b9m\u5206<\/pre>\n

    \u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\/var\/spool\/at\u6587\u4ef6\u4e2d\u67e5\u770bat\u7684\u5f53\u524d\u547d\u4ee4\u3002\u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0clinux\u9ed8\u8ba4atd\u8fdb\u7a0b\u5173\u95ed\u72b6\u6001\uff0c\u9700\u8981\u624b\u52a8\u6253\u5f00\u3002<\/p>\n

    crontab\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1<\/strong><\/div>\n

    linux\u5185\u7f6e\u7684cron\u8fdb\u7a0b\u80fd\u5e2e\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e9b\u9700\u6c42\uff0ccron\u642d\u914dshell\u811a\u672c\uff0c\u975e\u5e38\u590d\u6742\u7684\u6307\u4ee4\u4e5f\u6ca1\u6709\u95ee\u9898\u3002<\/p>\n

    cron\u4ecb\u7ecd<\/strong><\/span><\/div>\n

    cron\u5b88\u62a4\u8fdb\u7a0b\u662f\u4e00\u4e2a\u7531\u5b9e\u7528\u7a0b\u5e8f\u548c\u914d\u7f6e\u6587\u4ef6\u7ec4\u6210\u7684\u5c0f\u578b\u5b50\u7cfb\u7edf\uff0c\u5728\u51e0\u4e4e\u6240\u6709\u7c7b UNIX \u7cfb\u7edf\u4e0a\u90fd\u53ef\u4ee5\u627e\u5230\u67d0\u79cd\u98ce\u683c\u7684cron\uff0c\u6211\u4eec\u53ef\u4ee5\u7528ps aux|grep cron\u627e\u5230crond\u8fd9\u4e2a\u5b88\u62a4\u8fdb\u7a0b\u3002<\/p>\n

    \u6211\u4eec\u7ecf\u5e38\u4f7f\u7528\u7684\u662fcrontab\u547d\u4ee4\u662fcron table\u7684\u7b80\u5199\uff0c\u5b83\u662fcron\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u53eb\u5b83\u4f5c\u4e1a\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ee5\u4e0b\u6587\u4ef6\u5939\u5185\u627e\u5230\u76f8\u5173\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n

  • \/var\/spool\/cron\/ \u76ee\u5f55\u4e0b\u5b58\u653e\u7684\u662f\u6bcf\u4e2a\u7528\u6237\u5305\u62ecroot\u7684crontab\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u4ee5\u521b\u5efa\u8005\u7684\u540d\u5b57\u547d\u540d\u3002<\/li>\n
  • \/etc\/crontab \u8fd9\u4e2a\u6587\u4ef6\u8d1f\u8d23\u8c03\u5ea6\u5404\u79cd\u7ba1\u7406\u548c\u7ef4\u62a4\u4efb\u52a1\u3002<\/li>\n
  • \/etc\/cron.d\/ \u8fd9\u4e2a\u76ee\u5f55\u7528\u6765\u5b58\u653e\u4efb\u4f55\u8981\u6267\u884c\u7684crontab\u6587\u4ef6\u6216\u811a\u672c\u3002<\/li>\n

    \u6211\u4eec\u8fd8\u53ef\u4ee5\u628a\u811a\u672c\u653e\u5728\/etc\/con.hourly\u3001\/etc\/con.daily\u3001\/etc\/con.weekly\u3001\/etc\/con.monthly\u76ee\u5f55\u4e2d\uff0c\u8ba9\u5b83\u6bcf\u5c0f\u65f6\/\u5929\/\u661f\u671f\u3001\u6708\u6267\u884c\u4e00\u6b21\u3002<\/p>\n

    crontab\u7684\u4f7f\u7528<\/strong><\/span><\/div>\n

    \u6211\u4eec\u5e38\u7528\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n

    crontab [-u username]\u3000\u3000\u3000\u3000\/\/\u7701\u7565\u7528\u6237\u8868\u8868\u793a\u64cd\u4f5c\u5f53\u524d\u7528\u6237\u7684crontab\r\n    -e      (\u7f16\u8f91\u5de5\u4f5c\u8868)\r\n    -l      (\u5217\u51fa\u5de5\u4f5c\u8868\u91cc\u7684\u547d\u4ee4)\r\n    -r      (\u5220\u9664\u5de5\u4f5c\u4f5c)<\/pre>\n

    \u6211\u4eec\u7528crontab -e\u8fdb\u5165\u5f53\u524d\u7528\u6237\u7684\u5de5\u4f5c\u8868\u7f16\u8f91\uff0c\u662f\u5e38\u89c1\u7684vim\u754c\u9762\u3002\u6bcf\u884c\u662f\u4e00\u6761\u547d\u4ee4\u3002<\/p>\n

    crontab\u7684\u547d\u4ee4\u6784\u6210\u4e3a \u65f6\u95f4+\u52a8\u4f5c\uff0c\u5176\u65f6\u95f4\u6709\u5206\u3001\u65f6\u3001\u65e5\u3001\u6708\u3001\u5468\u4e94\u79cd\uff0c\u64cd\u4f5c\u7b26\u6709<\/p>\n

  • * \u53d6\u503c\u8303\u56f4\u5185\u7684\u6240\u6709\u6570\u5b57<\/li>\n
  • \/ \u6bcf\u8fc7\u591a\u5c11\u4e2a\u6570\u5b57<\/li>\n
  • - \u4eceX\u5230Z<\/li>\n
  • \uff0c\u6563\u5217\u6570\u5b57<\/li>\n

    \u4ee5\u4e0b\u662f\u51e0\u4e2a\u4f8b\u5b50\u3002<\/p>\n

    \u65f6\u95f4                  \u6ce8\u91ca\r\n0 0 25 12 *     \/\/\u572812\u670825\u65e5\u76840\u65f60\u5206\r\n*\/5 * * * *     \/\/\u6bcf\u8fc75\u5206\u949f\r\n* 4-6 * * *     \/\/\u6bcf\u5929\u76844 5 6\u70b9\r\n* * * * 2\uff0c5     \/\/\u6bcf\u5468\u4e8c\u548c\u5468\u4e94<\/pre>\n
    \u914d\u5408\u7b80\u5355\u7684shell\u811a\u672c<\/strong><\/div>\n

    \u5982\u679c\u6211\u4eec\u7684\u547d\u4ee4\u6709\u903b\u8f91\u5224\u65ad\u7b49\u975e\u5e38\u590d\u6742\u7684\u64cd\u4f5c\u65f6\uff0c\u518d\u76f4\u63a5\u7f16\u8f91crontab\u5c31\u6709\u70b9\u56f0\u96be\u4e86\uff0c\u8fd9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528shell\u811a\u672c\u3002\u5176\u6765\u5386\uff0c\u5206\u7c7b\u5b9a\u4e49\u4e0e\u9898\u4e0d\u7b26\uff0c\u4e0d\u518d\u591a\u8bf4\uff0c\u6211\u4eec\u76f4\u63a5\u8bf4\u5b83\u7684\u7528\u6cd5\u3002<\/p>\n

    \u6211\u4eec\u7528vim \/usr\/sh\/test.sh\u6765\u4f7f\u7528vim\u7f16\u8f91\u4e00\u4e2ashell\u811a\u672c<\/p>\n

    #!\/bin\/sh           \/\/\u58f0\u660e\u5f00\u59cbshell\u811a\u672c\r\na = \"hello world\"   \/\/\u5b9a\u4e49\u4e00\u4e2ashell\u53d8\u91cf\r\necho $a             \/\/\u719f\u6089\u7684echo\uff0c\u8f93\u51faa\u53d8\u91cf<\/pre>\n

    \u7136\u540ecrontab -e\u7f16\u8f91crontab\uff0c\u6dfb\u52a0 *\/5 * * * * \/usr\/sh\/test.sh\u6bcf\u9694\u4e94\u5206\u949f\u8fd0\u884c\u4e00\u6b21test.sh\u811a\u672c\uff0c\u4e5f\u53ef\u4ee5\u7528 \/phppath\/php \/filepath\/test.php \u6765\u7528php\u8fdb\u7a0b\u6765\u6267\u884cphp\u7a0b\u5e8f\u3002<\/p>\n

    \u5982\u679c\u60a8\u89c9\u5f97\u672c\u535a\u6587\u5bf9\u60a8\u6709\u5e2e\u52a9\uff0c\u60a8\u53ef\u4ee5\u63a8\u8350\u6216\u5173\u6ce8\u6211\uff0c\u5982\u679c\u60a8\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u4e0b\u65b9\u7559\u8a00\u8ba8\u8bba\uff0c\u8c22\u8c22\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u5b9e\u73b0\u201c\u81ea\u52a8\u5316\u201d\u6709\u5982\u4e0b\u597d\u5904\uff1a \u8282\u7701\u4eba\u529b\uff0c\u4e00\u4e2a\u811a\u672c\u5c31\u591f\u4e86\u3002 \u5728\u591c\u665a\u81ea\u52a8\u6267\u884c\u53ef\u4ee5\u907f\u5f00\u7f51\u7ad9\u6d41\u91cf\u9ad8\u5cf0\u671f\uff0c\u4e0d\u5f71\u54cd\u7f51\u7ad9\u767d\u5929\u7684 […]<\/p>\n","protected":false},"author":310,"featured_media":249446,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-249438","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/249438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/310"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=249438"}],"version-history":[{"count":9,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/249438\/revisions"}],"predecessor-version":[{"id":249926,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/249438\/revisions\/249926"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/249446"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=249438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=249438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=249438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}