{"id":322,"date":"2011-07-02T18:21:40","date_gmt":"2011-07-02T18:21:40","guid":{"rendered":"http:\/\/hgtas.com\/?p=322"},"modified":"2011-07-02T18:21:40","modified_gmt":"2011-07-02T18:21:40","slug":"introduction-to-linux-cron-task-system-scheduled","status":"publish","type":"post","link":"https:\/\/508.me\/?p=322","title":{"rendered":"Linux\u5b9a\u65f6\u4efb\u52a1\u7cfb\u7edfCron\u5165\u95e8"},"content":{"rendered":"<p>cron\u662f\u4e00\u4e2alinux\u4e0b\u7684\u5b9a\u65f6\u6267\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u65e0\u9700\u4eba\u5de5\u5e72\u9884\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u4f5c\u4e1a\u3002\u7531\u4e8eCron \u662fLinux\u7684\u5185\u7f6e\u670d\u52a1\uff0c\u4f46\u5b83\u4e0d\u81ea\u52a8\u8d77\u6765\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u7684\u65b9\u6cd5\u542f\u52a8\u3001\u5173\u95ed\u8fd9\u4e2a\u670d\u52a1\uff1a<\/p>\n<p>\/sbin\/service crond start \/\/\u542f\u52a8\u670d\u52a1<\/p>\n<p>\/sbin\/service crond stop \/\/\u5173\u95ed\u670d\u52a1<\/p>\n<p>\/sbin\/service crond restart \/\/\u91cd\u542f\u670d\u52a1<\/p>\n<p>\/sbin\/service crond reload \/\/\u91cd\u65b0\u8f7d\u5165\u914d\u7f6e<\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u670d\u52a1\u5728\u7cfb\u7edf\u542f\u52a8\u7684\u65f6\u5019\u81ea\u52a8\u542f\u52a8\uff1a<\/p>\n<p>\u5728\/etc\/rc.d\/rc.local\u8fd9\u4e2a\u811a\u672c\u7684\u672b\u5c3e\u52a0\u4e0a\uff1a<\/p>\n<p>\/sbin\/service crond start<\/p>\n<p>\u73b0\u5728Cron\u8fd9\u4e2a\u670d\u52a1\u5df2\u7ecf\u5728\u8fdb\u7a0b\u91cc\u9762\u4e86\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7528\u8fd9\u4e2a\u670d\u52a1\u4e86\uff0cCron\u670d\u52a1\u63d0\u4f9b\u4ee5\u4e0b\u51e0\u79cd\u63a5\u53e3\u4f9b\u5927\u5bb6\u4f7f\u7528\uff1a<\/p>\n<p><strong>1.\u76f4\u63a5\u7528crontab\u547d\u4ee4\u7f16\u8f91<\/strong><\/p>\n<p>cron\u670d\u52a1\u63d0\u4f9bcrontab\u547d\u4ee4\u6765\u8bbe\u5b9acron\u670d\u52a1\u7684\uff0c\u4ee5\u4e0b\u662f\u8fd9\u4e2a\u547d\u4ee4\u7684\u4e00\u4e9b\u53c2\u6570\u4e0e\u8bf4\u660e\uff1a<\/p>\n<p>crontab -u \/\/\u8bbe\u5b9a\u67d0\u4e2a\u7528\u6237\u7684cron\u670d\u52a1\uff0c\u4e00\u822croot\u7528\u6237\u5728\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\u7684\u65f6\u5019\u9700\u8981\u6b64\u53c2\u6570<\/p>\n<p>crontab -l \/\/\u5217\u51fa\u67d0\u4e2a\u7528\u6237cron\u670d\u52a1\u7684\u8be6\u7ec6\u5185\u5bb9<\/p>\n<p>crontab -r \/\/\u5220\u9664\u6ca1\u4e2a\u7528\u6237\u7684cron\u670d\u52a1<\/p>\n<p>crontab -e \/\/\u7f16\u8f91\u67d0\u4e2a\u7528\u6237\u7684cron\u670d\u52a1<\/p>\n<p>\u6bd4\u5982\u8bf4root\u67e5\u770b\u81ea\u5df1\u7684cron\u8bbe\u7f6e\uff1acrontab -u root -l<\/p>\n<p>\u518d\u4f8b\u5982\uff0croot\u60f3\u5220\u9664fred\u7684cron\u8bbe\u7f6e\uff1acrontab -u fred -r<\/p>\n<p>\u5728\u7f16\u8f91cron\u670d\u52a1\u65f6\uff0c\u7f16\u8f91\u7684\u5185\u5bb9\u6709\u4e00\u4e9b\u683c\u5f0f\u548c\u7ea6\u5b9a\uff0c\u8f93\u5165\uff1acrontab -u root -e<\/p>\n<p>\u8fdb\u5165vi\u7f16\u8f91\u6a21\u5f0f\uff0c\u7f16\u8f91\u7684\u5185\u5bb9\u4e00\u5b9a\u8981\u7b26\u5408\u4e0b\u9762\u7684\u683c\u5f0f\uff1a*\/1 * * * * ls &gt;&gt; \/tmp\/ls.txt<\/p>\n<p>\u8fd9\u4e2a\u683c\u5f0f\u7684\u524d\u4e00\u90e8\u5206\u662f\u5bf9\u65f6\u95f4\u7684\u8bbe\u5b9a\uff0c\u540e\u9762\u4e00\u90e8\u5206\u662f\u8981\u6267\u884c\u7684\u547d\u4ee4\uff0c\u5982\u679c\u8981\u6267\u884c\u7684\u547d\u4ee4\u592a\u591a\uff0c\u53ef\u4ee5\u628a\u8fd9\u4e9b\u547d\u4ee4\u5199\u5230\u4e00\u4e2a\u811a\u672c\u91cc\u9762\uff0c\u7136\u540e\u5728\u8fd9\u91cc\u76f4\u63a5\u8c03\u7528\u8fd9\u4e2a\u811a \u672c\u5c31\u53ef\u4ee5\u4e86\uff0c\u8c03\u7528\u7684\u65f6\u5019\u8bb0\u5f97\u5199\u51fa\u547d\u4ee4\u7684\u5b8c\u6574\u8def\u5f84\u3002\u65f6\u95f4\u7684\u8bbe\u5b9a\u6211\u4eec\u6709\u4e00\u5b9a\u7684\u7ea6\u5b9a\uff0c\u524d\u9762\u4e94\u4e2a*\u53f7\u4ee3\u8868\u4e94\u4e2a\u6570\u5b57\uff0c\u6570\u5b57\u7684\u53d6\u503c\u8303\u56f4\u548c\u542b\u4e49\u5982\u4e0b\uff1a<\/p>\n<p>\u5206\u949f\u3000\uff080-59\uff09<\/p>\n<p>\u5c0f\u6642\u3000\uff080-23\uff09<\/p>\n<p>\u65e5\u671f\u3000\uff081-31\uff09<\/p>\n<p>\u6708\u4efd\u3000\uff081-12\uff09<\/p>\n<p>\u661f\u671f\u3000\uff080-6\uff09\/\/0\u4ee3\u8868\u661f\u671f\u5929<\/p>\n<p>\u9664\u4e86\u6570\u5b57\u8fd8\u6709\u51e0\u4e2a\u4e2a\u7279\u6b8a\u7684\u7b26\u53f7\u5c31\u662f&#8221;*&#8221;\u3001&#8221;\/&#8221;\u548c&#8221;-&#8220;\u3001&#8221;,&#8221;\uff0c*\u4ee3\u8868\u6240\u6709\u7684\u53d6\u503c\u8303\u56f4\u5185\u7684\u6570\u5b57\uff0c&#8221;\/&#8221;\u4ee3\u8868\u6bcf\u7684\u610f\u601d,&#8221;*\/5&#8243;\u8868\u793a\u6bcf5\u4e2a\u5355\u4f4d\uff0c&#8221;-&#8220;\u4ee3\u8868\u4ece\u67d0\u4e2a\u6570\u5b57\u5230\u67d0\u4e2a\u6570\u5b57,&#8221;,&#8221;\u5206\u5f00\u51e0\u4e2a\u79bb\u6563\u7684\u6570\u5b57\u3002\u4ee5\u4e0b\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u8bf4\u660e\u95ee\u9898\uff1a<\/p>\n<p>\u6bcf\u5929\u65e9\u4e0a6\u70b9<\/p>\n<p>0 6 * * * echo &#8220;Good morning.&#8221; &gt;&gt; \/tmp\/test.txt \/\/\u6ce8\u610f\u5355\u7eafecho\uff0c\u4ece\u5c4f\u5e55\u4e0a\u770b\u4e0d\u5230\u4efb\u4f55\u8f93\u51fa\uff0c\u56e0\u4e3acron\u628a\u4efb\u4f55\u8f93\u51fa\u90fdemail\u5230root\u7684\u4fe1\u7bb1\u4e86\u3002<\/p>\n<p>\u6bcf\u4e24\u4e2a\u5c0f\u65f6<\/p>\n<p>0 *\/2 * * * echo &#8220;Have a break now.&#8221; &gt;&gt; \/tmp\/test.txt<\/p>\n<p>\u665a\u4e0a11\u70b9\u5230\u65e9\u4e0a8\u70b9\u4e4b\u95f4\u6bcf\u4e24\u4e2a\u5c0f\u65f6\uff0c\u65e9\u4e0a\u516b\u70b9<\/p>\n<p>0 23-7\/2\uff0c8 * * * echo &#8220;Have a good dream\uff1a\uff09&#8221; &gt;&gt; \/tmp\/test.txt<\/p>\n<p>\u6bcf\u4e2a\u6708\u76844\u53f7\u548c\u6bcf\u4e2a\u793c\u62dc\u7684\u793c\u62dc\u4e00\u5230\u793c\u62dc\u4e09\u7684\u65e9\u4e0a11\u70b9<\/p>\n<p>0 11 4 * 1-3 command line<\/p>\n<p>1\u67081\u65e5\u65e9\u4e0a4\u70b9<\/p>\n<p>0 4 1 1 * command line<\/p>\n<p>\u6bcf\u6b21\u7f16\u8f91\u5b8c\u67d0\u4e2a\u7528\u6237\u7684cron\u8bbe\u7f6e\u540e\uff0ccron\u81ea\u52a8\u5728\/var\/spool\/cron\u4e0b\u751f\u6210\u4e00\u4e2a\u4e0e\u6b64\u7528\u6237\u540c\u540d\u7684\u6587\u4ef6\uff0c\u6b64\u7528\u6237\u7684cron\u4fe1\u606f\u90fd\u8bb0\u5f55\u5728 \u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u8fd9\u4e2a\u6587\u4ef6\u662f\u4e0d\u53ef\u4ee5\u76f4\u63a5\u7f16\u8f91\u7684\uff0c\u53ea\u53ef\u4ee5\u7528crontab -e  \u6765\u7f16\u8f91\u3002cron\u542f\u52a8\u540e\u6bcf\u8fc7\u4e00\u4efd\u949f\u8bfb\u4e00\u6b21\u8fd9\u4e2a\u6587\u4ef6\uff0c\u68c0\u67e5\u662f\u5426\u8981\u6267\u884c\u91cc\u9762\u7684\u547d\u4ee4\u3002\u56e0\u6b64\u6b64\u6587\u4ef6\u4fee\u6539\u540e\u4e0d\u9700\u8981\u91cd\u65b0\u542f\u52a8cron\u670d\u52a1\u3002<\/p>\n<p><strong>2.\u7f16\u8f91\/etc\/crontab \u6587\u4ef6\u914d\u7f6ecron<\/strong><\/p>\n<p>cron\u670d\u52a1\u6bcf\u5206\u949f\u4e0d\u4ec5\u8981\u8bfb\u4e00\u6b21\/var\/spool\/cron\u5185\u7684\u6240\u6709\u6587\u4ef6\uff0c\u8fd8\u9700\u8981\u8bfb\u4e00\u6b21\/etc\/crontab,\u56e0\u6b64\u6211\u4eec\u914d\u7f6e\u8fd9\u4e2a\u6587\u4ef6\u4e5f\u80fd\u8fd0 \u7528 cron\u670d\u52a1\u505a\u4e00\u4e9b\u4e8b\u60c5\u3002\u7528crontab\u914d\u7f6e\u662f\u9488\u5bf9\u67d0\u4e2a\u7528\u6237\u7684\uff0c\u800c\u7f16\u8f91\/etc\/crontab\u662f\u9488\u5bf9\u7cfb\u7edf\u7684\u4efb\u52a1\u3002\u6b64\u6587\u4ef6\u7684\u6587\u4ef6\u683c\u5f0f\u662f\uff1a<\/p>\n<p>SHELL=\/bin\/bash<\/p>\n<p>PATH=\/sbin:\/bin:\/usr\/sbin:\/usr\/bin<\/p>\n<p>MAILTO=root \/\/\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff0c\u6216\u8005\u6709\u6570\u636e\u8f93\u51fa\uff0c\u6570\u636e\u4f5c\u4e3a\u90ae\u4ef6\u53d1\u7ed9\u8fd9\u4e2a\u5e10\u53f7<\/p>\n<p>HOME=\/ \/\/\u4f7f\u7528\u8005\u8fd0\u884c\u7684\u8def\u5f84,\u8fd9\u91cc\u662f\u6839\u76ee\u5f55<\/p>\n<p># run-parts<\/p>\n<p>01 * * * * root run-parts \/etc\/cron.hourly \/\/\u6bcf\u5c0f\u65f6\u6267\u884c\/etc\/cron.hourly\u5185\u7684\u811a\u672c<\/p>\n<p>02 4 * * * root run-parts \/etc\/cron.daily \/\/\u6bcf\u5929\u6267\u884c\/etc\/cron.daily\u5185\u7684\u811a\u672c<\/p>\n<p>22 4 * * 0 root run-parts \/etc\/cron.weekly \/\/\u6bcf\u661f\u671f\u6267\u884c\/etc\/cron.weekly\u5185\u7684\u811a\u672c<\/p>\n<p>42 4 1 * * root run-parts \/etc\/cron.monthly \/\/\u6bcf\u6708\u53bb\u6267\u884c\/etc\/cron.monthly\u5185\u7684\u811a\u672c<\/p>\n<p>\u5927\u5bb6\u6ce8\u610f&#8221;run-parts&#8221;\u8fd9\u4e2a\u53c2\u6570\u4e86\uff0c\u5982\u679c\u53bb\u6389\u8fd9\u4e2a\u53c2\u6570\u7684\u8bdd\uff0c\u540e\u9762\u5c31\u53ef\u4ee5\u5199\u8981\u8fd0\u884c\u7684\u67d0\u4e2a\u811a\u672c\u540d\uff0c\u800c\u4e0d\u662f\u6587\u4ef6\u5939\u540d\u4e86\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/os.51cto.com\/art\/200512\/13558.htm\">http:\/\/os.51cto.com\/art\/200512\/13558.htm<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>cron\u662f\u4e00\u4e2alinux\u4e0b\u7684\u5b9a\u65f6\u6267\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u65e0\u9700\u4eba\u5de5\u5e72\u9884\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u4f5c\u4e1a\u3002\u7531\u4e8eCron \u662fLinux\u7684\u5185\u7f6e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[96],"tags":[98,22,99],"_links":{"self":[{"href":"https:\/\/508.me\/index.php?rest_route=\/wp\/v2\/posts\/322"}],"collection":[{"href":"https:\/\/508.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/508.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/508.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/508.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=322"}],"version-history":[{"count":2,"href":"https:\/\/508.me\/index.php?rest_route=\/wp\/v2\/posts\/322\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/508.me\/index.php?rest_route=\/wp\/v2\/posts\/322\/revisions\/324"}],"wp:attachment":[{"href":"https:\/\/508.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/508.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/508.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}