{"id":232300,"date":"2021-12-27T09:43:58","date_gmt":"2021-12-27T01:43:58","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=232300"},"modified":"2021-12-19T19:44:34","modified_gmt":"2021-12-19T11:44:34","slug":"linux-jieshi-corntab","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-jieshi-corntab.html","title":{"rendered":"Linux \u9ed1\u8bdd\u89e3\u91ca\uff1a\u4ec0\u4e48\u662f\u5b9a\u65f6\u4efb\u52a1"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u672c\u671f\u7684\u300aLinux \u9ed1\u8bdd\u89e3\u91ca\u300b\u7cfb\u5217\u6587\u7ae0\u4e2d\uff0c\u4f60\u5c06\u4e86\u89e3\u5230 Linux \u4e2d\u7684\u5b9a\u65f6\u4efb\u52a1\u529f\u80fd\u3002\u4f60\u5c06\u901a\u8fc7\u5b66\u4e60\u7f16\u8f91 crontab \u6587\u4ef6\u6765\u521b\u5efa\u5b9a\u65f6\u4efb\u52a1\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/a><\/p>\n

\u4f55\u4e3a\u5b9a\u65f6\u4efb\u52a1<\/strong><\/div>\n

cron\u662f\u4e00\u4e2a\u7528\u4e8e\u6309\u8ba1\u5212\u8fd0\u884c\u77ed\u5c0f\u4e14\u5feb\u901f\u7684\u547d\u4ee4\u7684\u5b9e\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3002\u8be5\u5de5\u5177\u662f\u4e00\u4e2a\u65b9\u4fbf\u3001\u7ecf\u5178\u7684\u7cfb\u7edf\u7ba1\u7406\u5de5\u5177\uff0c\u901a\u8fc7\u548c\u5176\u4ed6\u5de5\u5177\u7ed3\u5408\u4f7f\u7528\u53ef\u4ee5\u81ea\u52a8\u8fd0\u884c\u5404\u5f0f\u5404\u6837\u7684\u4efb\u52a1\u3002\u6bd4\u5982\uff0c\u6709\u4e9b\u4eba\u901a\u8fc7\u628arsync\u548ccron\u7ed3\u5408\u4f7f\u7528\uff0c\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u81ea\u52a8\u521b\u5efa\u6bcf\u65e5\u5907\u4efd\u548c\u6bcf\u5468\u5907\u4efd\u3002\u4e5f\u6709\u4e9b\u4eba\u4f7f\u7528cron\u6765\u5206\u6790\u670d\u52a1\u5668\u65e5\u5fd7\uff0c\u5e76\u4e14\u7ed3\u5408\u90ae\u4ef6\u7cfb\u7edf\u529f\u80fd\uff0c\u5728\u65e5\u5fd7\u68c0\u6d4b\u5230\u9519\u8bef\u65f6\u81ea\u52a8\u53d1\u9001\u544a\u8b66\u90ae\u4ef6\u3002
\ncron\u5c31\u5982\u540c\u201c\u745e\u58eb\u519b\u5200\u201d\u4e00\u6837\uff0c\u53ef\u4ee5\u591a\u573a\u666f\u591a\u6837\u5316\u4f7f\u7528\u3002\u5c3d\u53ef\u80fd\u53d1\u6325\u4f60\u7684\u60f3\u8c61\uff0c\u53bb\u6316\u6398\u5b83\u7684\u529f\u80fd\u3002
\n\u5176\u5b9ecron\u7684\u4f7f\u7528\u5f88\u5bb9\u6613\u4e0a\u624b\uff0c\u53ea\u9700\u8981\u51e0\u79d2\u949f\u3002\u4e0d\u8fc7\u5728\u6211\u4eec\u5f00\u59cb\u4e0a\u624b\u4e4b\u524d\uff0c\u5148\u6765\u8ba8\u8bba\u4e0b\u51e0\u4e2a\u7ecf\u5e38\u5bb9\u6613\u6df7\u6dc6\u7684\u6982\u5ff5\u3002<\/p>\n

cron\u3001\u5b9a\u65f6\u4efb\u52a1\u3001crontab<\/strong><\/div>\n

\u6709\u4e09\u4e2a\u672f\u8bed\u6bd4\u8f83\u5bb9\u6613\u6df7\u6dc6\uff1acron\u3001\u5b9a\u65f6\u4efb\u52a1\u548ccrontab\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5b83\u4eec\u7684\u542b\u4e49\uff1a<\/p>\n\n\n\n\n\n\n\n
\u672f\u8bed<\/th>\n\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n
cron<\/td>\n\u8fd9\u662f\u5b89\u88c5\u5728\u7cfb\u7edf\u4e0a\u7684\u5b9e\u9645\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u7684\u00a0\u5b88\u62a4\u8fdb\u7a0b\u3002<\/td>\n<\/tr>\n
\u5b9a\u65f6\u4efb\u52a1<\/td>\n\u201c\u4efb\u52a1\u201d\u662f\u6307\u4e00\u6bb5\u542f\u52a8\u5e76\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002 cron \u00a0\u53ef\u4ee5\u6309\u7167\u7ea6\u5b9a\u7684\u65f6\u95f4\u8ba1\u5212\u8fd0\u884c\u5404\u79cd\u4efb\u52a1\uff0c\u8fd9\u6837\u7684\u4efb\u52a1\u901a\u5e38\u53eb\u505a\u201c\u5b9a\u65f6\u4efb\u52a1\u201d\u3002<\/td>\n<\/tr>\n
crontab<\/td>\n\u8fd9\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u5b9a\u65f6\u4efb\u52a1\u3002\u4e00\u4e2a\u00a0 crontab \u00a0\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u8868\u683c\u5f62\u5f0f\uff08\u6bcf\u4e00\u884c\u5c31\u662f\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\uff09\u5b9a\u4e49\u591a\u4e2a\u5b9a\u65f6\u4efb\u52a1\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50\uff1a\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\uff0c\u6bcf\u5c0f\u65f6\u5411crontabl_log.txt\u6587\u4ef6\u6253\u5370Linux is cool!\u3002<\/p>\n

0 * * * * echo \"Linux is Cool!\" >> ~\/crontab_log.txt<\/pre>\n

\u662f\u4e0d\u662f\u8fd9\u4e48\u4e2a\u7b80\u5355\u5b9a\u65f6\u4efb\u52a1\u7684\u4f8b\u5b50\u90fd\u8ba9\u4f60\u611f\u5230\u60ca\u6050\uff0c\u8fd9\u662f\u56e0\u4e3a\u4f60\u9700\u8981\u61c2\u5f97\u5982\u4f55\u53bb\u8bfb\u61c2\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u7684\u5c5e\u6027\u3002
\n\u6211\u5c06\u5728\u540e\u6587\u4e2d\u8bb2\u8ff0\u8fd9\u4e2a\u57fa\u7840\u7406\u8bba\u77e5\u8bc6\u3002<\/p>\n

\u4e0a\u624b cron<\/strong><\/span><\/div>\n

\u6211\u4eec\u901a\u8fc7\u53e6\u4e00\u4e2a\u4f8b\u5b50\u6765\u770b\u770bcron\u5982\u4f55\u5de5\u4f5c\u3002
\n\u4e3a\u4e86\u521b\u5efa\u5b9a\u65f6\u4efb\u52a1\uff08\u6216\u8005\u8bf4cron\u5c06\u8981\u6267\u884c\u7684\u547d\u4ee4\u4efb\u52a1\uff09\uff0c\u4f60\u53ea\u9700\u8981\u8fd0\u884c\uff1a<\/p>\n

crontab -e\r\n<\/pre>\n

\u8fd9\u5c06\u4f1a\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u7528\u4e8e\u7f16\u8f91\u5b9a\u65f6\u4efb\u52a1\uff1a
\n\"\"<\/a>
\n\u5176\u4e2d\uff0c\u6240\u6709\u4ee5#\u5f00\u5934\u7684\u884c\u90fd\u662f\u6ce8\u91ca\uff0c\u7528\u4e8e\u6307\u5bfc\u4f60\u5982\u4f55\u4f7f\u7528cron\uff0c\u5982\u679c\u89c9\u5f97\u6ca1\u7528\u53ef\u4ee5\u5220\u9664\u5b83\u4eec\u3002<\/p>\n

\u6211\u4eec\u5c06\u521b\u5efa\u5982\u4e0b\u4efb\u52a1\uff0c\u4f5c\u4e3a\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\uff1a<\/p>\n

* * * * * touch ~\/crontab_test<\/pre>\n

\u8ba9\u6211\u5feb\u901f\u770b\u770b\u8be5\u4efb\u52a1\u5c06\u4f1a\u505a\u4ec0\u4e48\uff1a
\n\u5b9a\u65f6\u4efb\u52a1\u90fd\u662f\u4ee5 \u201c\u5206\u949f \u5c0f\u65f6 \u5929 \u6708 \u5468 \u547d\u4ee4\u201d \u5f62\u5f0f\u5448\u73b0\uff1a
\n\u5206\u949f\uff1a\u6307\u8be5\u4efb\u52a1\u5728\u54ea\u4e00\u5206\u949f\u4f1a\u88ab\u6267\u884c\u3002\u6240\u4ee5\uff0c\u8be5\u503c\u4e3a0\u5219\u4ee3\u8868\u5728\u6bcf\u4e2a\u5c0f\u65f6\u5f00\u59cb\u65f6\u8fd0\u884c\uff0c5\u5219\u4ee3\u8868\u5728\u6bcf\u4e2a\u5c0f\u65f6\u7684\u7b2c 5 \u5206\u949f\u4f1a\u8fd0\u884c\u3002
\n\u5c0f\u65f6\uff1a\u6307\u8be5\u4efb\u52a1\u5728\u4e00\u5929\u4e2d\u7684\u54ea\u4e2a\u5c0f\u65f6\u4f1a\u88ab\u6267\u884c\uff0c\u53d6\u503c\u8303\u56f4\u4e3a0-23\u3002\u6ca1\u670924\u7684\u539f\u56e0\u662f23\u65f6\u7684\u672b\u5c3e\u662f\u534a\u591c11:59\uff0c\u7136\u540e\u5c31\u662f\u6bcf\u5929\u7684\u5f00\u59cb0\u65f6\u3002\u5206\u949f\u7684\u53d6\u503c\u8303\u56f4\u5b9a\u4e49\u903b\u8f91\u4e0e\u4e4b\u7c7b\u4f3c\u3002
\n\u5929\uff1a\u6307\u4e00\u4e2a\u6708\u4e2d\u7684\u54ea\u4e00\u5929\u6267\u884c\u8be5\u4efb\u52a1\uff0c\u53d6\u503c\u8303\u56f4\u662f1-31\uff08\u4e0d\u540c\u4e8e\u524d\u9762\u7684\u5206\u949f\u548c\u5c0f\u65f6\u4ece0\u5f00\u59cb\u53d6\u503c\uff09\u3002
\n\u6708\uff1a\u6307\u8be5\u4efb\u52a1\u5728\u54ea\u4e2a\u6708\u88ab\u6267\u884c\uff0c\u53d6\u503c\u8303\u56f4\u662f1-12\u3002
\n\u5468\uff1a\u6307\u8be5\u4efb\u52a1\u5728\u661f\u671f\u51e0\u88ab\u6267\u884c\uff0c\u4ece\u5468\u65e5\u5f00\u59cb\u7b97\u8d77\uff0c\u53d6\u503c\u8303\u56f4\u662f0-6\uff08\u5206\u522b\u5bf9\u5e94\u5468\u65e5\u3001\u5468\u4e00\u5230\u5468\u516d\uff09\u3002
\n\u547d\u4ee4\uff1a\u662f\u4f60\u60f3\u8981\u8fd0\u884c\u7684\u547d\u4ee4\u4efb\u52a1\u3002
\n
\"\"<\/a>
\n\u5982\u679c\u60f3\u5bf9 \u201c\u5206\u949f \u5c0f\u65f6 \u5929 \u6708 \u5468\u201d \u90e8\u5206\u6709\u66f4\u8be6\u7ec6\u7684\u7406\u89e3\uff0c\u53ef\u4ee5\u53c2\u8003Crontab guru \u7f51\u7ad9\uff0c\u8be5\u7f51\u7ad9\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7406\u89e3\u6b63\u5728\u6267\u884c\u4ec0\u4e48\u3002
\n\u63a5\u7740\u4e4b\u524d\u7684\u4f8b\u5b50* * * * * touch ~\/crontab_test<\/span>\uff0c\u8868\u793a\u6bcf\u5206\u949f\u521b\u5efa\u4e00\u6b21~\/crontab_test<\/span>\u6587\u4ef6\u3002
\n\u8ba9\u6211\u4eec\u5c06\u8be5\u4efb\u52a1\u7f16\u8f91\u8fdbcrontab\u7136\u540e\u770b\u770b\u6267\u884c\u7ed3\u679c\uff1a
\n\u7b49\u5230\u4e0b\u4e00\u5206\u949f\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0\u4f60\u7684\u5bb6\u76ee\u5f55\u4e0b\u591a\u4e86\u6587\u4ef6crontab_test:
\n\u8fd9\u4fbf\u662fcron\u7684\u57fa\u7840\u5e94\u7528\u793a\u4f8b\u3002<\/p>\n

\u4e00\u4e2a\u5b9e\u7528\u7684\u5b9a\u65f6\u4efb\u52a1\u793a\u4f8b<\/strong><\/span><\/div>\n

\u5047\u8bbe\u4f60\u60f3\u521b\u5efa\u4e00\u4e2a\u811a\u672c\uff0c\u7528\u4e8e\u62f7\u8d1d\u591a\u4e2a\u76ee\u5f55\u5185\u5bb9\u5230\u4e00\u4e2a\u8def\u5f84\u5e76\u6253\u5305\u4f5c\u4e3a\u5907\u4efd\uff0c\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f
\n\u901a\u8fc7cron\u5b9a\u65f6\u4efb\u52a1\u5c31\u53ef\u4ee5\u5f88\u5bb9\u6613\u5b9e\u73b0\u8be5\u529f\u80fd\u3002
\n\u8bf7\u770b\u5982\u4e0b\u811a\u672c\uff1a<\/p>\n

#!\/usr\/bin\/bash\r\necho \"Backing up...\"\r\nmkdir -p ~\/.local\/tmp\/\r\ntar -Pc ~\/Documents\/ -f ~\/.local\/tmp\/backup.gz\r\n<\/pre>\n

\u8be5\u811a\u672c\u505a\u4e86\u5982\u4e0b\u4e8b\u60c5\uff1a<\/p>\n

\u786e\u4fdd\u5907\u4efd\u8def\u5f84\u76ee\u5f55~\/.local\/tmp\/<\/span>\u5b58\u5728\u3002
\n\u5c06\u76ee\u5f55~\/Documents\/<\/span>\u4e0b\u7684\u6240\u6709\u5185\u5bb9\u6253\u5305\u81f3\u6587\u4ef6~\/.local\/tmp\/backup.gz<\/span>\u3002<\/p>\n

\u6211\u4eec\u5148\u6765\u624b\u52a8\u8fd0\u884c\u8be5\u811a\u672c\uff0c\u770b\u770b\u5b83\u5230\u5e95\u5982\u4f55\u5de5\u4f5c\u3002
\n\u9996\u5148\uff0c\u6211\u4eec\u5728\u5bb6\u76ee\u5f55\uff08~\uff09\u4e0b\u521b\u5efa\u8be5\u811a\u672c\uff0c\u547d\u4ee4\u4e3abackup_script\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a
\n\u7136\u540e\u7f16\u8f91backup_script\u811a\u672c\uff0c\u5199\u5165\u4e0a\u9762\u90a3\u4e2a\u811a\u672c\u4ee3\u7801\u3002
\n\u63a5\u7740\uff0c\u8d4b\u4e88backup_script\u53ef\u6267\u884c\u6743\u9650\uff1a
\n\u6700\u540e\u8fd0\u884c\u811a\u672c~\/backup_script<\/span>\uff0c\u8fdb\u884c\u529f\u80fd\u9a8c\u8bc1\uff1a
\n\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u547d\u4ee4tar -xf ~\/.local\/tmp\/backup.gz -C <output_dir><\/span>\u6765\u8fdb\u884c\u5907\u4efd\u6062\u590d\uff0c\u8fd9\u91cc<output_dir><\/span>;\u662f\u6307\u6587\u4ef6\u8981\u6062\u590d\u5230\u7684\u8def\u5f84\u76ee\u5f55\u3002
\n\u63a5\u4e0b\u6765\uff0c\u5c31\u53ef\u4ee5\u7528cron\u5de5\u5177\u6765\u8fdb\u884c\u5b9a\u65f6\u4efb\u52a1\u8fd0\u884c\u8be5\u811a\u672c\u4e86\u3002
\n\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u9700\u8981\u6bcf\u5929\u7684\u51cc\u6668 3 \u70b9\u8fd0\u884c\u8be5\u5907\u4efd\u811a\u672c\uff0c\u4f60\u53ef\u4ee5\u5728crontab\u4e2d\u8f93\u5165\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n

* 3 * * * ~\/backup_script<\/pre>\n

\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u6bcf\u5929\u81ea\u52a8\u8fdb\u884c\u5907\u4efd\u64cd\u4f5c\u4e86\u3002<\/p>\n

\u540e\u8bb0<\/strong><\/div>\n

\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u4e86\u5b9a\u65f6\u4efb\u52a1\u529f\u80fd\u3002\u5c3d\u7ba1\u6211\u4e0d\u786e\u5b9a Linux \u684c\u9762\u7528\u6237\u4f7f\u7528\u8be5\u529f\u80fd\u591a\u4e0d\u591a\uff0c\u4f46\u6211\u77e5\u9053\u5b9a\u65f6\u4efb\u52a1\u529f\u80fd\u88ab\u8bb8\u591a\u7cfb\u7edf\u7ba1\u7406\u5458\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

cron\u662f\u4e00\u4e2a\u7528\u4e8e\u6309\u8ba1\u5212\u8fd0\u884c\u77ed\u5c0f\u4e14\u5feb\u901f\u7684\u547d\u4ee4\u7684\u5b9e\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3002\u8be5\u5de5\u5177\u662f\u4e00\u4e2a\u65b9\u4fbf\u3001\u7ecf\u5178\u7684\u7cfb\u7edf\u7ba1\u7406\u5de5\u5177\uff0c\u901a\u8fc7\u548c\u5176\u4ed6 […]<\/p>\n","protected":false},"author":643,"featured_media":232312,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-232300","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/232300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=232300"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/232300\/revisions"}],"predecessor-version":[{"id":232311,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/232300\/revisions\/232311"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/232312"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=232300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=232300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=232300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}