{"id":1076,"date":"2020-07-13T15:54:44","date_gmt":"2020-07-13T07:54:44","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=1076"},"modified":"2020-07-13T15:54:44","modified_gmt":"2020-07-13T07:54:44","slug":"ubuntu-cron-%e5%ae%9a%e6%97%b6%e6%89%a7%e8%a1%8c%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=1076","title":{"rendered":"Ubuntu cron \u5b9a\u65f6\u6267\u884c\u4efb\u52a1"},"content":{"rendered":"\n<p><strong>\u5173\u4e8ecrontab<\/strong><br>cron\u662f\u4e00\u4e2aLinux\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\u5728Ubuntu server \u4e0b\uff0ccron\u662f\u88ab\u9ed8\u8ba4\u5b89\u88c5\u5e76\u542f\u52a8\u7684\u3002\u901a\u8fc7\/etc\/crontab\u6587\u4ef6\uff0c\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload-images.jianshu.io\/upload_images\/311426-de6671ae514491ee.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/717\/format\/webp\" alt=\"\"\/><\/figure>\n\n\n\n<p>ununtu \u901a\u8fc7\u8c03\u7528 run-parts \u547d\u4ee4\uff0c\u5b9a\u65f6\u8fd0\u884c\u56db\u4e2a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u811a\u672c\u30021\uff09\/etc\/cron.hourly\uff0c\u76ee\u5f55\u4e0b\u7684\u811a\u672c\u4f1a\u6bcf\u4e2a\u5c0f\u65f6\u8ba9\u6267\u884c\u4e00\u6b21\uff0c\u5728\u6bcf\u5c0f\u65f6\u76842\u5206\u949f\u65f6\u8fd0\u884c\uff1b<br>2\uff09\/etc\/cron.daily\uff0c\u76ee\u5f55\u4e0b\u7684\u811a\u672c\u4f1a\u6bcf\u5929\u8ba9\u6267\u884c\u4e00\u6b21\uff0c\u5728\u6bcf\u59290\u70b917\u5206\u65f6\u8fd0\u884c\uff1b3\uff09\/etc\/cron.weekly\uff0c\u76ee\u5f55\u4e0b\u7684\u811a\u672c\u4f1a\u6bcf\u5468\u8ba9\u6267\u884c\u4e00\u6b21\uff0c\u5728\u6bcf\u5468\u7b2c\u4e03\u5929\u76843\u70b956\u5206\u65f6\u8fd0\u884c\uff1b<br>4\uff09\/etc\/cron.mouthly\uff0c\u76ee\u5f55\u4e0b\u7684\u811a\u672c\u4f1a\u6bcf\u6708\u8ba9\u6267\u884c\u4e00\u6b21\uff0c\u5728\u6bcf\u670819\u53f7\u76845\u70b932\u5206\u65f6\u8fd0\u884c\uff1b<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0a\u7684\u65f6\u95f4\u5747\u662f\u7cfb\u7edf\u9ed8\u8ba4\u65f6\u95f4\uff0c\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n\n\n\n<p><strong>cron \u670d\u52a1\u7684\u542f\u52a8\u4e0e\u505c\u6b62<\/strong><\/p>\n\n\n\n<p>\u5728Ubuntu 9.10\u4e0b\uff0ccron\u662f\u88ab\u9ed8\u8ba4\u5b89\u88c5\u5e76\u542f\u52a8\u7684\u3002\u800c ubuntu \u4e0b\u542f\u52a8\uff0c\u505c\u6b62\u4e0e\u91cd\u542fcron\uff0c\u5747\u662f\u901a\u8fc7\u8c03\u7528\/etc\/init.d\/\u4e2d\u7684\u811a\u672c\u8fdb\u884c\u3002\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>1\uff09service&nbsp;cron&nbsp;start&nbsp;&nbsp;\/*\u542f\u52a8\u670d\u52a1*\/<\/p>\n\n\n\n<p>2\uff09service&nbsp;cron&nbsp;stop \/*\u5173\u95ed\u670d\u52a1*\/<\/p>\n\n\n\n<p>3\uff09service cron restart \/*\u91cd\u542f\u670d\u52a1*\/<\/p>\n\n\n\n<p>4\uff09service cron reload \/*\u91cd\u65b0\u8f7d\u5165\u914d\u7f6e*\/<\/p>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770bcron\u662f\u5426\u5728\u8fd0\u884c\uff08\u5982\u679c\u5728\u8fd0\u884c\uff0c\u5219\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fdb\u7a0bID\uff09\uff1a<\/p>\n\n\n\n<p># pgrep cron<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload-images.jianshu.io\/upload_images\/311426-4ab2bdd33b5d2be6.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/716\/format\/webp\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>crontab\u547d\u4ee4<\/strong><\/p>\n\n\n\n<p>crontab \u547d\u4ee4\u7528\u4e8e\u5b89\u88c5\u3001\u5220\u9664\u6216\u8005\u5217\u51fa\u7528\u4e8e\u9a71\u52a8cron\u540e\u53f0\u8fdb\u7a0b\u7684\u8868\u683c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7528\u6237\u628a\u9700\u8981\u6267\u884c\u7684\u547d\u4ee4\u5e8f\u5217\u653e\u5230crontab\u6587\u4ef6\u4e2d\u4ee5\u83b7\u5f97\u6267\u884c\uff0c\u6bcf\u4e2a\u7528\u6237\u90fd\u53ef\u4ee5\u6709\u81ea\u5df1\u7684crontab\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u8fd9\u4e2a\u547d\u4ee4\u7684\u4e00\u4e9b\u53c2\u6570\u4e0e\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p>1\uff09crontab -u \/*\u8bbe\u5b9a\u67d0\u4e2a\u7528\u6237\u7684cron\u670d\u52a1*\/<\/p>\n\n\n\n<p>2\uff09crontab -l \/*\u5217\u51fa\u67d0\u4e2a\u7528\u6237cron\u670d\u52a1\u7684\u8be6\u7ec6\u5185\u5bb9*\/<\/p>\n\n\n\n<p>3\uff09crontab -r \/*\u5220\u9664\u67d0\u4e2a\u7528\u6237\u7684cron\u670d\u52a1*\/<\/p>\n\n\n\n<p>4\uff09crontab -e \/*\u7f16\u8f91\u67d0\u4e2a\u7528\u6237\u7684cron\u670d\u52a1*\/<\/p>\n\n\n\n<p>\u53c2\u6570\u540d\u79f0 \u542b\u4e49 \u793a\u4f8b<\/p>\n\n\n\n<p>-l \u663e\u793a\u7528\u6237\u7684Crontab\u6587\u4ef6\u7684\u5185\u5bb9 crontabl \u2013l<\/p>\n\n\n\n<p>-i \u5220\u9664\u7528\u6237\u7684Crontab\u6587\u4ef6\u524d\u7ed9\u63d0\u793a crontabl -ri<\/p>\n\n\n\n<p>-r \u4eceCrontab\u76ee\u5f55\u4e2d\u5220\u9664\u7528\u6237\u7684Crontab\u6587\u4ef6 crontabl -r<\/p>\n\n\n\n<p>-e \u7f16\u8f91\u7528\u6237\u7684Crontab\u6587\u4ef6 crontabl -e<\/p>\n\n\n\n<p>\/etc\/crontab\u6587\u4ef6\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>Minute Hour Day Month Dayofweek command<\/p>\n\n\n\n<p>\u5206\u949f \u5c0f\u65f6 \u5929 \u6708 \u5929\u6bcf\u661f\u671f \u547d\u4ee4<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u5b57\u6bb5\u4ee3\u8868\u7684\u542b\u4e49\u53ca\u53d6\u503c\u8303\u56f4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>Minute \uff1a\u5206\u949f\uff080-59\uff09\uff0c\u8868\u793a\u6bcf\u4e2a\u5c0f\u65f6\u7684\u7b2c\u51e0\u5206\u949f\u6267\u884c\u8be5\u4efb\u52a1<\/p>\n\n\n\n<p>Hour \uff1a \u5c0f\u65f6\uff081-23\uff09\uff0c\u8868\u793a\u6bcf\u5929\u7684\u7b2c\u51e0\u4e2a\u5c0f\u65f6\u6267\u884c\u8be5\u4efb\u52a1<\/p>\n\n\n\n<p>Day \uff1a \u65e5\u671f\uff081-31\uff09\uff0c\u8868\u793a\u6bcf\u6708\u7684\u7b2c\u51e0\u5929\u6267\u884c\u8be5\u4efb\u52a1<\/p>\n\n\n\n<p>Month \uff1a \u6708\u4efd\uff081-12\uff09\uff0c\u8868\u793a\u6bcf\u5e74\u7684\u7b2c\u51e0\u4e2a\u6708\u6267\u884c\u8be5\u4efb\u52a1<\/p>\n\n\n\n<p>DayOfWeek \uff1a \u661f\u671f\uff080-6\uff0c0\u4ee3\u8868\u661f\u671f\u5929\uff09\uff0c\u8868\u793a\u6bcf\u5468\u7684\u7b2c\u51e0\u5929\u6267\u884c\u8be5\u4efb\u52a1<\/p>\n\n\n\n<p>Command \uff1a \u6307\u5b9a\u8981\u6267\u884c\u7684\u547d\u4ee4\uff08\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\uff09<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e9b\u5b57\u6bb5\u91cc\uff0c\u9664\u4e86\u201cCommand\u201d\u662f\u6bcf\u6b21\u90fd\u5fc5\u987b\u6307\u5b9a\u7684\u5b57\u6bb5\u4ee5\u5916\uff0c\u5176\u5b83\u5b57\u6bb5\u7686\u4e3a\u53ef\u9009\u5b57\u6bb5\uff0c\u53ef\u89c6\u9700\u8981\u51b3\u5b9a\u3002\u5bf9\u4e8e\u4e0d\u6307\u5b9a\u7684\u5b57\u6bb5\uff0c\u8981\u7528\u201c*\u201d\u6765\u586b\u8865\u5176\u4f4d\u7f6e\u3002\u540c\u65f6\uff0ccron\u652f\u6301\u7c7b\u4f3c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e66\u5199\uff0c\u652f\u6301\u5982\u4e0b\u51e0\u4e2a\u7279\u6b8a\u7b26\u53f7\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<p>\u201c*\u201d ,\u4ee3\u8868\u6240\u6709\u7684\u53d6\u503c\u8303\u56f4\u5185\u7684\u6570\u5b57\uff1b<\/p>\n\n\n\n<p>\u201c\/\u201d , \u4ee3\u8868\u201d\u6bcf\u201d\uff08\u201c*\/5\u201d\uff0c\u8868\u793a\u6bcf5\u4e2a\u5355\u4f4d\uff09\uff1b<\/p>\n\n\n\n<p>\u201c-\u201d , \u4ee3\u8868\u4ece\u67d0\u4e2a\u6570\u5b57\u5230\u67d0\u4e2a\u6570\u5b57\uff08\u201c1-4\u201d\uff0c\u8868\u793a1-4\u4e2a\u5355\u4f4d\uff09\uff1b<\/p>\n\n\n\n<p>\u201c,\u201d ,\u5206\u5f00\u51e0\u4e2a\u79bb\u6563\u7684\u6570\u5b57\uff1b<\/p>\n\n\n\n<p>\u4e3e\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>5 * * * * ls \/*\u6307\u5b9a\u6bcf\u5c0f\u65f6\u7684\u7b2c5\u5206\u949f\u6267\u884c\u4e00\u6b21ls\u547d\u4ee4*\/<\/p>\n\n\n\n<p>30 5 * * * ls \/*\u6307\u5b9a\u6bcf\u5929\u7684 5:30 \u6267\u884cls\u547d\u4ee4*\/<\/p>\n\n\n\n<p>30 7 8 * * ls \/*\u6307\u5b9a\u6bcf\u67088\u53f7\u76847\uff1a30\u5206\u6267\u884cls\u547d\u4ee4*\/<\/p>\n\n\n\n<p>50 7 * * * root run-parts \/etc\/cron.daily \/*\u6bcf\u59297\uff1a50\u4ee5root \u8eab\u4efd\u6267\u884c\/etc\/cron.daily\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u53ef\u6267\u884c\u6587\u4ef6*\/<\/p>\n\n\n\n<p><strong>\u65b0\u589e cron \u4efb\u52a1<\/strong><\/p>\n\n\n\n<p>1.\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u6dfb\u52a0\u4efb\u52a1<\/p>\n\n\n\n<p># crontab -e<\/p>\n\n\n\n<p>1\uff09\u76f4\u63a5\u6267\u884c\u547d\u4ee4\u884c\uff0c\u6bd4\u5982\u6bcf\u96941\u5206\u949f\u6267\u884cdate\u547d\u4ee4\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u81f3\u6587\u4ef6\/tmp\/testCron.txt\u4e2d\uff0ccron \u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>*\/1 * * * * date &gt;&gt; \/tmp\/testCron.txt<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload-images.jianshu.io\/upload_images\/311426-60a37f505773bd00.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/716\/format\/webp\" alt=\"\"\/><\/figure>\n\n\n\n<p>2\uff09\u6267\u884cshell \u811a\u672c\uff0c\u6bd4\u5982\u6bcf\u96943\u5206\u949f\u6267\u884c\u4e00\u6b21\/var\/backups\/test.sh&nbsp;\u6587\u4ef6\uff0ccron \u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>*\/3 * * * * \/var\/backups\/test.sh<\/p>\n\n\n\n<p>\u6587\u4ef6&nbsp;\/var\/backups\/test.sh \u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<\/p>\n\n\n\n<p>#!\/bin\/sh<\/p>\n\n\n\n<p>cd&nbsp;\/var\/backups\/<\/p>\n\n\n\n<p>Now=`date&nbsp;\u2018+%Y%m%d\u2019`<\/p>\n\n\n\n<p>tar&nbsp;-zcf&nbsp;\/var\/backups\/${Now}Website.tar.gz&nbsp;\/var\/www\/html\/<\/p>\n\n\n\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<\/p>\n\n\n\n<p>2. \u91cd\u542fcron \u670d\u52a1<\/p>\n\n\n\n<p># service cron restart<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload-images.jianshu.io\/upload_images\/311426-d422d795b3741a7a.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/715\/format\/webp\" alt=\"\"\/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8ecrontabcron\u662f\u4e00\u4e2aLinux\u5b9a\u65f6\u6267\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u65e0\u9700\u4eba\u5de5\u5e72\u9884\u7684\u60c5\u51b5 <span class=\"readmore\"><a href=\"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=1076\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-1076","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1076"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1076\/revisions"}],"predecessor-version":[{"id":1077,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1076\/revisions\/1077"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1076"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}