{"id":230487,"date":"2021-11-24T08:22:39","date_gmt":"2021-11-24T00:22:39","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=230487"},"modified":"2021-11-19T11:22:58","modified_gmt":"2021-11-19T03:22:58","slug":"linux-sleep-wait-ways","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-sleep-wait-ways.html","title":{"rendered":"Linux\u4e2dSleep\u548cWait\u547d\u4ee4\u7684\u4f7f\u7528\u65b9\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nLinux \u4e2d\u7684sleep\u548cwait\u547d\u4ee4\u5141\u8bb8\u4f60\u4ee5\u9009\u5b9a\u7684\u901f\u5ea6\u8fd0\u884c\u547d\u4ee4\u6216\u5728\u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\u540e\u6355\u83b7\u5e76\u663e\u793a\u4efb\u52a1\u7684\u9000\u51fa\u72b6\u6001\u3002sleep\u53ea\u662f\u5728\u547d\u4ee4\u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u5b9a\u65f6\u6682\u505c\u3002wait\u547d\u4ee4\u5728\u5b8c\u6210\u540e\u624d\u901a\u77e5\u5b83\u5df2\u5b8c\u6210\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
Sleep\u547d\u4ee4<\/strong><\/div>\n

sleep \u547d\u4ee4\u4f1a\u6682\u505c\u6307\u5b9a\u7684\u65f6\u95f4\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u811a\u672c\u4e2d\uff0c\u4f46\u4e5f\u9002\u7528\u4e8e\u547d\u4ee4\u884c\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c sleep \u5728\u4e24\u4e2a date \u547d\u4ee4\u4e4b\u95f4\u6682\u505c30\u79d2\u3002<\/p>\n

\r\n[root@localhost ~]# date ; sleep 30 ; date\r\nThu Nov 11 05:06:44 EST 2021\r\nThu Nov 11 05:07:14 EST 2021\r\n<\/pre>\n

\"\"
\nsleep \u547d\u4ee4\u5c06\u6570\u5b57\u53c2\u6570\u4f5c\u4e3a\u79d2\u6570\u3002\u4f46\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5728\u53c2\u6570\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5b57\u7b26\u6765\u8ba9\u5b83\u4f11\u7720\u4e0d\u540c\u7684\u65f6\u95f4\uff1a<\/p>\n

    \n
  • 10s = 10 seconds<\/li>\n
  • 1m = 1 minute<\/li>\n
  • 2h = 2 hours<\/li>\n
  • 3d = 3 days<\/li>\n<\/ul>\n
    \r\n[root@localhost ~]# date ; sleep 10s ; date\r\nThu Nov 11 05:11:09 EST 2021\r\nThu Nov 11 05:11:19 EST 2021\r\n<\/pre>\n

    \u7761\u7720\u65f6\u95f4\u4e5f\u53ef\u4ee5\u5c0f\u4e8e1\u79d2\u949f\uff0c\u5c31\u662f\u5728\u6570\u5b57\u524d\u9762\u52a0\u4e00\u4e2a.<\/code>.<\/p>\n

      \n
    • .1 = \u5341\u5206\u4e4b\u4e00\u79d2<\/li>\n
    • .01 = \u767e\u5206\u4e4b\u4e00\u79d2<\/li>\n
    • .001 = \u5343\u5206\u4e4b\u4e00\u79d2<\/li>\n<\/ul>\n
      \r\n[root@localhost ~]# date ; sleep .1 ; date\r\nThu Nov 11 05:14:03 EST 2021\r\nThu Nov 11 05:14:03 EST 2021\r\n<\/pre>\n

      sleep \u547d\u4ee4\u901a\u5e38\u7528\u4e8e\u5b9a\u671f\u8fd0\u884c\u547d\u4ee4\uff0c\u4f8b\u5982\u6bcf 10 \u79d2\u6216\u6bcf\u5206\u949f\u4e00\u6b21\uff0c\u5f53\u4f60\u60f3\u8981\u76d1\u89c6\u67d0\u4e9b\u6d3b\u52a8\u65f6\u3002\u4f8b\u5982\uff0c\u53ef\u80fd\u6b63\u5728\u7b49\u5f85\u540c\u4e8b\u767b\u5f55\u6587\u4ef6\u670d\u52a1\u5668\u3002\u60a8\u53ef\u80fd\u4f1a\u8fd0\u884c\u8fd9\u6837\u7684\u547d\u4ee4\uff1a<\/p>\n

      \r\n[root@localhost ~]# while true ; do who ; sleep 100 ; done\r\n<\/pre>\n

      \"\"
      \n\u53ef\u4ee5\u89c2\u5bdf\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7684\u53d8\u5316\u3002<\/p>\n

      \r\n[root@localhost ~]# while true ; do free -h ; sleep 5 ; done\r\n<\/pre>\n

      \"\"<\/p>\n

      Wait\u547d\u4ee4<\/strong><\/div>\n

      wait \u547d\u4ee4\u5728\u7b49\u5f85\u540e\u53f0\u8fdb\u7a0b\u5b8c\u6210\u540e\u6355\u83b7\u540e\u53f0\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u3002\u60a8\u4e5f\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u5b83\u3002\u5c06 sleep \u547d\u4ee4\u66ff\u6362\u4e3a\u8981\u7b49\u5f85\u7684\u8fdb\u7a0b\u3002\u6b64\u811a\u672c\u8fd8\u663e\u793a\u9000\u51fa\u72b6\u6001\u3002<\/p>\n

      \r\n#!\/bin\/bash\r\n\r\nsleep 5 &\r\nprocess_id=$!\r\necho \u201cPID: $process_id\u201d\r\nwait $process_id\r\necho \u201cExit status: $?\u201d\r\n<\/pre>\n

      \u7b2c\u4e00\u884c\u5728\u540e\u53f0\u542f\u52a8 sleep \u547d\u4ee4\u3002\u7b2c\u4e8c\u884c\u83b7\u53d6\u6700\u8fd1\u6267\u884c\u7684\u540e\u53f0\u8fdb\u7a0b\u7684\u8fdb\u7a0b ID$!<\/code>\u3002\u7136\u540e\u811a\u672c\u663e\u793a\u8be5\u4fe1\u606f\uff0c\u7b49\u5f85\u8be5\u8fdb\u7a0b\u5b8c\u6210\u5e76\u663e\u793a\u9000\u51fa\u72b6\u6001\u3002<\/p>\n

      \u5982\u679c\u4f7f\u7528 wait -n\uff08\u4e0d\u9700\u8981\u989d\u5916\u7684\u53c2\u6570\uff09\uff0cwait \u5c06\u7b49\u5f85\u4efb\u4f55\u540e\u53f0\u4efb\u52a1\u5b8c\u6210\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c sleep 6 \u547d\u4ee4\u9996\u5148\u5b8c\u6210\uff0c\u811a\u672c\u7ed3\u675f\uff0c\u4f46\u5176\u4ed6\u4e24\u4e2a sleep \u8fdb\u7a0b\u5c06\u7ee7\u7eed\u5728\u540e\u53f0\u8fd0\u884c\u51e0\u79d2\u949f\u3002<\/p>\n

      \r\n#!\/bin\/bash\r\n\r\nsleep 15 &\r\nsleep 9 &\r\nsleep 6 &\r\nwait -n\r\necho \u201cFirst job has been completed.\u201d\r\n<\/pre>\n

      \u5982\u679c\u6539\u6210\u4ee5\u4e0b\u811a\u672c\uff0c\u5b83\u4f1a\u5728\u6bcf\u4e2a\u7761\u7720\u8fc7\u7a0b\u5b8c\u6210\u65f6\u544a\u8bc9\u4f60\u3002\u7531\u4e8e\u65f6\u95f4\u5173\u7cfb\uff0c\u8fd9\u5c06\u6309\u7167\u4e0e\u7761\u7720\u8fc7\u7a0b\u5f00\u59cb\u65f6\u76f8\u53cd\u7684\u987a\u5e8f\u53d1\u751f\u3002<\/p>\n

      \r\n#!\/bin\/bash\r\n\r\nsleep 15 &\r\nsleep 9 &\r\nsleep 6 &\r\nwait -n\r\necho \u201cFirst job has been completed.\u201d\r\nwait -n\r\necho \u201cNext job has been completed.\u201d\r\nwait\r\necho \u201cAll jobs have been completed.\u201d\r\n<\/pre>\n

      \u6ce8\u610f\uff1a\u7b49\u5f85\u7684\u8fdb\u7a0b\u5fc5\u987b\u662f\u201c\u5b50\u8fdb\u7a0b\u201d\uff08\u5728\u5f53\u524d shell \u4e2d\u542f\u52a8\u7684\u8fdb\u7a0b\uff09\u3002<\/p>\n

      \u603b\u7ed3<\/strong><\/div>\n

      \u4e24\u8005\u90fd\u53ef\u4ee5\u5728\u811a\u672c\u6216\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528\u3002\u4f46\u662f\uff0cwait \u547d\u4ee4\u4ec5\u9002\u7528\u4e8e\u5728\u540e\u53f0\u8fd0\u884c\u7684\u8fdb\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

      sleep \u547d\u4ee4\u4f1a\u6682\u505c\u6307\u5b9a\u7684\u65f6\u95f4\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u811a\u672c\u4e2d\uff0c\u4f46\u4e5f\u9002\u7528\u4e8e\u547d\u4ee4\u884c\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c sleep \u5728\u4e24\u4e2a d […]<\/p>\n","protected":false},"author":309,"featured_media":230488,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-230487","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\/230487","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\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=230487"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/230487\/revisions"}],"predecessor-version":[{"id":230492,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/230487\/revisions\/230492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/230488"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=230487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=230487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=230487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}