{"id":476476,"date":"2023-08-09T07:29:55","date_gmt":"2023-08-09T07:29:55","guid":{"rendered":""},"modified":"2023-09-05T11:12:51","modified_gmt":"2023-09-05T11:12:51","slug":"cron","status":"publish","type":"wiki","link":"https:\/\/oneproxy.pro\/cn\/wiki\/cron\/","title":{"rendered":"\u514b\u7f57\u6069"},"content":{"rendered":"<p>Cron \u662f\u7c7b Unix \u64cd\u4f5c\u7cfb\u7edf\u4e2d\u57fa\u4e8e\u65f6\u95f4\u7684\u4f5c\u4e1a\u8c03\u5ea6\u670d\u52a1\u3002\u7528\u6237\u53ef\u4ee5\u5b89\u6392\u4f5c\u4e1a\uff08\u547d\u4ee4\u6216\u811a\u672c\uff09\u5728\u56fa\u5b9a\u7684\u65f6\u95f4\u3001\u65e5\u671f\u6216\u95f4\u9694\u5b9a\u671f\u8fd0\u884c\u3002<\/p>\n<h2>Cron \u7684\u8d77\u6e90\u548c\u65e9\u671f\u5386\u53f2<\/h2>\n<p>cron \u7684\u9996\u6b21\u5b9e\u73b0\u53ef\u4ee5\u8ffd\u6eaf\u5230\u7b2c 7 \u7248 Unix\u3002\u672f\u8bed\u201ccron\u201d\u6765\u81ea\u5e0c\u814a\u8bed\u4e2d\u7684\u65f6\u95f4\u201cchronos\u201d\u30021979 \u5e74\u53d1\u5e03\u7684\u8fd9\u4e2a\u7248\u672c\u7684 Unix \u6709\u4e00\u4e2a\u7b80\u5355\u7684 cron \u7a0b\u5e8f\uff0c\u53ef\u4ee5\u6309\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u6267\u884c\u4efb\u52a1\u3002Cron \u7684\u529f\u80fd\u540e\u6765\u7531 Paul Vixie \u4e8e 1987 \u5e74\u6269\u5c55\u3002\u8fd9\u4e2a\u7248\u672c\u88ab\u79f0\u4e3a Vixie Cron\uff0c\u662f\u76ee\u524d\u6700\u5e38\u5b89\u88c5\u7684\u7248\u672c\uff0c\u4e5f\u662f\u4eba\u4eec\u63d0\u5230\u201cCron\u201d\u65f6\u901a\u5e38\u63d0\u5230\u7684\u7248\u672c\u3002<\/p>\n<h2>\u6269\u5c55 Cron \u7684\u6982\u5ff5<\/h2>\n<p>Cron \u5141\u8bb8\u7528\u6237\u81ea\u52a8\u6267\u884c\u7cfb\u7edf\u7ef4\u62a4\u6216\u7ba1\u7406 \u2014 \u5c3d\u7ba1\u5b83\u7684\u901a\u7528\u6027\u8d28\u4f7f\u5176\u53ef\u7528\u4e8e\u4ece Internet \u4e0b\u8f7d\u6587\u4ef6\u548c\u5b9a\u671f\u4e0b\u8f7d\u7535\u5b50\u90ae\u4ef6\u7b49\u64cd\u4f5c\u3002cron \u73af\u5883\u5c31\u50cf\u4e00\u4e2a\u6c99\u7bb1\uff0c\u5176\u4e2d\u7684 cron \u4f5c\u4e1a\u5728\u5e38\u89c4 shell \u73af\u5883\u4e4b\u5916\u8fd0\u884c\u3002<\/p>\n<p>\u6bcf\u4e2a\u7528\u6237\u90fd\u53ef\u4ee5\u62e5\u6709\u81ea\u5df1\u7684 crontab\uff0c\u5c3d\u7ba1\u8fd9\u4e9b\u6587\u4ef6\u4f4d\u4e8e \/var\/spool\/ \u4e2d\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u65e8\u5728\u76f4\u63a5\u7f16\u8f91\u3002 cron \u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u4e3b\u8981\u8bed\u6cd5\u7c7b\u578b\uff1aUnix cron \u8bed\u6cd5\u548c\u4e00\u79cd\u66f4\u590d\u6742\u7684\u8bed\u6cd5\uff0c\u79f0\u4e3a cron \u8868\u8fbe\u5f0f\u8bed\u6cd5\u6216 quartz cron \u8bed\u6cd5\u3002<\/p>\n<h2>Cron \u7684\u5185\u90e8\u7ed3\u6784\u53ca\u5176\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>Cron \u8fd0\u884c\u57fa\u4e8e\u4e00\u4e2a\u7b80\u5355\u4f46\u529f\u80fd\u5f3a\u5927\u7684\u914d\u7f6e\u6587\u4ef6\u201ccrontab\u201d\u3002\u6b64\u6587\u4ef6\u5305\u542b\u6bcf\u884c\u8981\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u7531\u4e94\u4e2a\u65f6\u95f4\u548c\u65e5\u671f\u5b57\u6bb5\u7ec4\u6210\uff0c\u540e\u9762\u8ddf\u7740\u8981\u5728\u6307\u5b9a\u65f6\u95f4\u8fd0\u884c\u7684\u547d\u4ee4\u3002<\/p>\n<p>\u8fd9\u4e94\u4e2a\u5b57\u6bb5\u4ee3\u8868\uff1a<\/p>\n<ol>\n<li>\u6574\u70b9\u540e\u7684\u5206\u949f\u6570 (0 \u2013 59)<\/li>\n<li>\u4e00\u5929\u4e2d\u7684\u5c0f\u65f6\u6570 (0 \u2013 23)<\/li>\n<li>\u6bcf\u6708\u5929\u6570 (1 \u2013 31)<\/li>\n<li>\u4e00\u5e74\u4e2d\u7684\u6708\u4efd\uff081 \u2013 12\uff09<\/li>\n<li>\u4e00\u5468\u4e2d\u7684\u65e5\u5b50\uff080 - 7\uff0c\u5176\u4e2d 0 \u548c 7 \u8868\u793a\u661f\u671f\u65e5\uff09<\/li>\n<\/ol>\n<p>\u547d\u4ee4\u5b57\u6bb5\u5305\u542b\u5728\u524d\u4e94\u4e2a\u5b57\u6bb5\u6307\u5b9a\u7684\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u3002<\/p>\n<h2>Cron \u7684\u4e3b\u8981\u7279\u70b9<\/h2>\n<p>Cron \u7684\u4e3b\u8981\u529f\u80fd\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\u5b83\u5141\u8bb8\u4efb\u52a1\u5728\u540e\u53f0\u5b9a\u671f\u81ea\u52a8\u8fd0\u884c\u3002<\/li>\n<li>\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7075\u6d3b\u7684\u5e73\u53f0\u6765\u4f7f\u7528\u591a\u79cd\u65f6\u95f4\u5355\u4f4d\u6765\u5b89\u6392\u4efb\u52a1\uff08\u811a\u672c\u6216\u547d\u4ee4\uff09\u3002<\/li>\n<li>\u6bcf\u4e2a\u7528\u6237\u90fd\u53ef\u4ee5\u62e5\u6709\u81ea\u5df1\u7684 crontab \u6587\u4ef6\u3002<\/li>\n<li>Cron \u4f5c\u4e1a\u53ef\u4ee5\u6309\u5206\u949f\u3001\u5c0f\u65f6\u3001\u661f\u671f\u51e0\u3001\u6bcf\u6708\u7684\u67d0\u5929\u548c\u6bcf\u5e74\u7684\u67d0\u6708\u5b89\u6392\u8fd0\u884c\u3002<\/li>\n<li>Cron\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u6709\u6548\u7684\u65b9\u6cd5\u6765\u81ea\u52a8\u6267\u884c\u5404\u79cd\u7cfb\u7edf\u4efb\u52a1\u3002<\/li>\n<\/ol>\n<h2>Cron \u7684\u7c7b\u578b<\/h2>\n<p>Cron \u4efb\u52a1\u901a\u5e38\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u51e0\u7c7b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u91cd\u542f<\/td>\n<td>\u542f\u52a8\u65f6\u8fd0\u884c\u4e00\u6b21\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u963f\u7eb3\u514b\u9686<\/td>\n<td>\u6309\u7167\u5929\u6570\u6307\u5b9a\u7684\u95f4\u9694\u8fd0\u884c\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u7cfb\u7edf\u8ba1\u5212\u4efb\u52a1<\/td>\n<td>\u8fd9\u4e9b\u5b58\u50a8\u5728 \/etc\/crontab \u548c \/etc\/cron.d\/ \u76ee\u5f55\u4e2d\uff0c\u65e8\u5728\u7528\u4e8e\u7cfb\u7edf\u8303\u56f4\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u7528\u6237\u8ba1\u5212\u4efb\u52a1<\/td>\n<td>\u6bcf\u4e2a\u7528\u6237\u90fd\u53ef\u4ee5\u62e5\u6709\u81ea\u5df1\u7684\uff0c\u5b83\u4eec\u5b58\u50a8\u5728 \/var\/spool\/cron\/crontabs\/ \u4e2d\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Cron \u7684\u4f7f\u7528\u3001\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>Cron \u5bf9\u4e8e\u81ea\u52a8\u6267\u884c\u4efb\u52a1\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5982\u679c\u4f7f\u7528\u4e0d\u5f53\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u914d\u7f6e\u9519\u8bef\u7684 cron \u4f5c\u4e1a\u53ef\u80fd\u4f1a\u5bfc\u81f4 CPU \u4f7f\u7528\u7387\u8fc7\u9ad8\u3002\u6b64\u5916\uff0ccron \u53ea\u4f1a\u5c06\u5176\u4f5c\u4e1a\u7684\u8f93\u51fa\uff08\u5982\u679c\u6709\uff09\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u7ed9\u4ee5\u8be5\u4f5c\u4e1a\u8eab\u4efd\u8fd0\u884c\u8be5\u4f5c\u4e1a\u7684\u7528\u6237\u3002\u5982\u679c\u4e0d\u5b9a\u671f\u68c0\u67e5\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9057\u6f0f\u9519\u8bef\u3002<\/p>\n<p>\u8fd9\u4e9b\u95ee\u9898\u7684\u5e38\u89c1\u89e3\u51b3\u65b9\u6848\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u5b9a\u671f\u68c0\u67e5 CPU \u4f7f\u7528\u7387\u662f\u5426\u8fc7\u9ad8\u5e76\u91cd\u65b0\u914d\u7f6e\u6216\u505c\u6b62\u4e0d\u5fc5\u8981\u7684\u4f5c\u4e1a\u3002<\/li>\n<li>\u5728\u5c06\u6240\u6709\u547d\u4ee4\u548c\u811a\u672c\u8bbe\u7f6e\u4e3a cron \u4f5c\u4e1a\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u5b83\u4eec\u6b63\u786e\u8fd0\u884c\u3002<\/li>\n<li>\u5c06\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u65e5\u5fd7\u6587\u4ef6\u6216\u8bbe\u7f6e\u90ae\u4ef6\u670d\u52a1\u5668\u4ee5\u66f4\u6709\u6548\u5730\u76d1\u63a7 cron \u4f5c\u4e1a\u3002<\/li>\n<\/ul>\n<h2>\u4e0e\u7c7b\u4f3c\u5de5\u5177\u7684\u6bd4\u8f83<\/h2>\n<p>\u8fd8\u6709\u5176\u4ed6\u4e0e cron \u7c7b\u4f3c\u7684\u4efb\u52a1\u8c03\u5ea6\u7a0b\u5e8f\uff0c\u4f8b\u5982 anacron \u548c systemd \u8ba1\u65f6\u5668\u3002\u5b83\u4eec\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u53ef\u4ee5\u6982\u62ec\u4e3a\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5de5\u5177<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u514b\u7f57\u6069<\/td>\n<td>\u9002\u5408\u5728\u4e00\u5929\u4e2d\u7684\u7279\u5b9a\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u3002\u4e0d\u4f1a\u8fd0\u884c\u9519\u8fc7\u7684\u4f5c\u4e1a\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u963f\u7eb3\u514b\u9686<\/td>\n<td>\u9002\u5408\u6bcf\u5929\/\u6bcf\u5468\/\u6bcf\u6708\u8fd0\u884c\u4e00\u6b21\u7684\u4efb\u52a1\u3002\u673a\u5668\u6062\u590d\u5728\u7ebf\u65f6\u5c06\u8fd0\u884c\u9519\u8fc7\u7684\u4f5c\u4e1a\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u7cfb\u7edf\u5316<\/td>\n<td>\u5b83\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u9009\u9879\u5e76\u4e14\u53ef\u4ee5\u4e0e\u73b0\u4ee3 Linux \u53d1\u884c\u7248\u5f88\u597d\u5730\u96c6\u6210\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u672a\u6765\u5c55\u671b\u53ca\u76f8\u5173\u6280\u672f<\/h2>\n<p>\u968f\u7740\u57fa\u4e8e Unix \u7684\u7cfb\u7edf\u4e0d\u65ad\u53d1\u5c55\uff0cCron \u4e5f\u5728\u4e0d\u65ad\u53d1\u5c55\u3002\u5c3d\u7ba1\u51fa\u73b0\u4e86\u50cf systemd \u8fd9\u6837\u7684\u66f4\u9ad8\u7ea7\u7684\u4efb\u52a1\u8c03\u5ea6\u7a0b\u5e8f\uff0c\u4f46 Cron \u7684\u7b80\u5355\u6027\u548c\u5f3a\u5927\u529f\u80fd\u786e\u4fdd\u5b83\u81f3\u4eca\u4ecd\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<p>\u8fd1\u671f\u5bb9\u5668\u5316\u5e94\u7528\u7a0b\u5e8f\u548c\u5fae\u670d\u52a1\u67b6\u6784\u7684\u6d41\u884c\u4e5f\u5e26\u6765\u4e86\u4f7f\u7528 Cron \u7684\u65b0\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u6d41\u884c\u7684\u5bb9\u5668\u7f16\u6392\u5e73\u53f0 Kubernetes \u5177\u6709\u4e00\u9879\u79f0\u4e3a CronJobs \u7684\u529f\u80fd\uff0c\u7c7b\u4f3c\u4e8e\u57fa\u4e8e Unix \u7684\u7cfb\u7edf\u4e2d Cron\u3002<\/p>\n<h2>\u4ee3\u7406\u670d\u52a1\u5668\u548c Cron<\/h2>\n<p>\u4ee3\u7406\u670d\u52a1\u5668\u53ef\u4ee5\u4e0e Cron \u7ed3\u5408\u4f7f\u7528\uff0c\u6709\u6548\u5730\u81ea\u52a8\u6267\u884c\u67d0\u4e9b\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u811a\u672c\u9700\u8981\u5b9a\u671f\u901a\u8fc7\u4ee3\u7406\u670d\u52a1\u5668\u8bbf\u95ee\u7f51\u7ad9\u6216 API\uff0c\u5219\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a cron \u4f5c\u4e1a\u4ee5\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u8fd0\u884c\u8be5\u811a\u672c\u3002<\/p>\n<p>Cron \u8fd8\u53ef\u7528\u4e8e\u6309\u6307\u5b9a\u95f4\u9694\u8f6e\u6362\u4ee3\u7406\uff0c\u4ece\u800c\u964d\u4f4e\u4efb\u4f55\u5355\u4e2a\u4ee3\u7406\u88ab\u963b\u6b62\u7684\u53ef\u80fd\u6027\u3002\u4f8b\u5982\uff0cOneProxy \u7684\u4ee3\u7406\u670d\u52a1\u53ef\u4ee5\u901a\u8fc7 Cron \u81ea\u52a8\u6267\u884c\u6570\u636e\u6293\u53d6\u3001\u81ea\u52a8\u6267\u884c\u793e\u4ea4\u5a92\u4f53\u4efb\u52a1\u7b49\u4efb\u52a1\u3002<\/p>\n<h2>\u76f8\u5173\u94fe\u63a5<\/h2>\n<p>\u6709\u5173 Cron \u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u4ee5\u4e0b\u8d44\u6e90\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/man7.org\/linux\/man-pages\/man5\/crontab.5.html\" target=\"_new\" rel=\"noopener nofollow\">Crontab \u624b\u518c\u9875<\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Cron\" target=\"_new\" rel=\"noopener nofollow\">\u7ef4\u57fa\u767e\u79d1\u4e0a\u7684 Cron<\/a><\/li>\n<li><a href=\"https:\/\/www.freebsd.org\/cgi\/man.cgi?query=cron&amp;sektion=8\" target=\"_new\" rel=\"noopener nofollow\">\u7ef4\u514b\u897f\u00b7\u514b\u6717<\/a><\/li>\n<\/ul>","protected":false},"featured_media":468042,"menu_order":0,"template":"","meta":{"_acf_changed":false,"content-type":"","inline_featured_image":false,"footnotes":""},"class_list":["post-476476","wiki","type-wiki","status-publish","has-post-thumbnail","hentry"],"acf":{"faq_title":"Frequently Asked Questions about <mark>Cron: A Comprehensive Guide<\/mark>","faq_items":[{"question":"What is Cron?","answer":"<p>Cron is a time-based job scheduling service found in Unix-like operating systems, which allows users to automate tasks to run periodically at fixed times, dates, or intervals.<\/p>"},{"question":"Where did Cron originate from?","answer":"<p>The first implementation of Cron dates back to Version 7 Unix, released in 1979. The term \"cron\" comes from the Greek word for time, \"chronos\". Cron's functionality was later expanded by Paul Vixie in 1987, with this version commonly referred to as \"Cron\" today.<\/p>"},{"question":"How does Cron work?","answer":"<p>Cron operates through a configuration file known as a \"crontab\" file. This file contains one line for each task to be run, consisting of five time and date fields, followed by a command to be run at the specified times.<\/p>"},{"question":"What are the key features of Cron?","answer":"<p>Cron allows tasks to run automatically in the background at regular intervals. It provides a flexible platform to schedule tasks with a variety of time units. Each user can have their own crontab file, and Cron jobs can be scheduled by the minute, hour, day of the week, day of the month, and month of the year.<\/p>"},{"question":"What types of Cron exist?","answer":"<p>Cron jobs can generally be categorized as Reboot, Anacron, System Cron, and User Cron.<\/p>"},{"question":"What are some common problems and solutions with using Cron?","answer":"<p>Common problems with using Cron include high CPU usage and overlooked errors due to Cron only emailing the output of its jobs. These issues can be mitigated by regularly checking for high CPU usage, ensuring commands and scripts run correctly before setting them as cron jobs, and setting up a mail server or redirecting output to a logfile for more effective monitoring.<\/p>"},{"question":"How does Cron compare to other similar tools?","answer":"<p>Cron is suitable for tasks to be run at specific times and does not run missed jobs. In contrast, Anacron is suitable for tasks to be run once daily\/weekly\/monthly and will run missed jobs when the machine is back online. Systemd offers more options and integrates well with modern Linux distributions, making it suitable for more complex scenarios.<\/p>"},{"question":"What is the future of Cron and related technologies?","answer":"<p>As Unix-based systems evolve, so does Cron. The trend towards containerized applications and microservices architectures has introduced new ways to use Cron, like Kubernetes' feature known as CronJobs.<\/p>"},{"question":"How can proxy servers be used with Cron?","answer":"<p>Proxy servers can be used with Cron to automate tasks, such as accessing a website or API through a proxy server at specified intervals. Cron can also be used to rotate proxies at intervals, reducing the chance of any single proxy being blocked.<\/p>"}]},"_links":{"self":[{"href":"https:\/\/oneproxy.pro\/cn\/wp-json\/wp\/v2\/wiki\/476476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oneproxy.pro\/cn\/wp-json\/wp\/v2\/wiki"}],"about":[{"href":"https:\/\/oneproxy.pro\/cn\/wp-json\/wp\/v2\/types\/wiki"}],"version-history":[{"count":0,"href":"https:\/\/oneproxy.pro\/cn\/wp-json\/wp\/v2\/wiki\/476476\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/oneproxy.pro\/cn\/wp-json\/wp\/v2\/media\/468042"}],"wp:attachment":[{"href":"https:\/\/oneproxy.pro\/cn\/wp-json\/wp\/v2\/media?parent=476476"}],"curies":[{"name":"\u53ef\u6e7f\u6027\u7c89\u5242","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}