{"id":242169,"date":"2022-05-16T09:00:04","date_gmt":"2022-05-16T01:00:04","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=242169"},"modified":"2022-05-09T11:00:50","modified_gmt":"2022-05-09T03:00:50","slug":"linux-watch-tail","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-watch-tail.html","title":{"rendered":"\u5de7\u7528watch \u548c tail \u547d\u4ee4\u76d1\u89c6 Linux \u4e0a\u7684\u6d3b\u52a8"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u4ecb\u7ecd\u4e86\u8fd9\u4e24\u4e2a\u547d\u4ee4\u7684\u4e00\u4e9b\u6709\u7528\u7684\u4f7f\u7528\u65b9\u6cd5\u3002watch \u548c tail \u547d\u4ee4\u4e3a\u6301\u7eed\u76d1\u89c6 Linux \u7cfb\u7edf\u4e0a\u7684\u6d3b\u52a8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u8da3\u7684\u9009\u9879\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

watch \u548c tail \u547d\u4ee4\u53ef\u4ee5\u5e2e\u52a9\u76d1\u89c6 Linux \u7cfb\u7edf\u4e0a\u7684\u6d3b\u52a8\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u8fd9\u4e24\u4e2a\u547d\u4ee4\u7684\u4e00\u4e9b\u6709\u7528\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n

watch \u548c tail \u547d\u4ee4\u4e3a\u6301\u7eed\u76d1\u89c6 Linux \u7cfb\u7edf\u4e0a\u7684\u6d3b\u52a8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u8da3\u7684\u9009\u9879\u3002<\/p>\n

\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 watch \u6765\u663e\u793a\u8c01\u5df2\u767b\u5f55\uff0c\u5e76\u968f\u7740\u7528\u6237\u767b\u5f55\u548c\u6ce8\u9500\u4e0d\u65ad\u66f4\u65b0\uff0c\u800c\u4e0d\u662f\u4ec5\u4ec5\u63d0\u51fa\u95ee\u9898\u5e76\u83b7\u5f97\u7b54\u6848(\u4f8b\u5982\u8be2\u95ee who \u5e76\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7528\u6237\u7684\u5217\u8868)\u3002<\/p>\n

\u4f7f\u7528 tail\uff0c\u4f60\u53ef\u4ee5\u663e\u793a\u6587\u4ef6\u7684\u5e95\u90e8\u5e76\u5728\u6dfb\u52a0\u5185\u5bb9\u65f6\u67e5\u770b\u5185\u5bb9\u3002\u8fd9\u79cd\u76d1\u63a7\u4e00\u822c\u975e\u5e38\u6709\u7528\uff0c\u5e76\u4e14\u6bd4\u5b9a\u671f\u8fd0\u884c\u547d\u4ee4\u6240\u9700\u7684\u5de5\u4f5c\u66f4\u5c11\u3002<\/p>\n

\u4f7f\u7528 watch \u547d\u4ee4<\/strong><\/div>\n

\u4f7f\u7528 watch \u7684\u6700\u7b80\u5355\u793a\u4f8b\u4e4b\u4e00\u662f\u4f7f\u7528\u547d\u4ee4 watch who\u3002\u4f60\u4f1a\u770b\u5230\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u663e\u793a\u4e86\u8c01\u767b\u5f55\u4e86\uff0c\u4ee5\u53ca\u4ed6\u4eec\u767b\u5f55\u7684\u65f6\u95f4\u548c\u767b\u5f55\u4f4d\u7f6e\u3002\u8bf7\u6ce8\u610f\uff0c\u9ed8\u8ba4\u8bbe\u7f6e\u662f\u6bcf\u4e24\u79d2\u66f4\u65b0\u4e00\u6b21\u663e\u793a(\u5de6\u4e0a\u89d2)\uff0c\u65e5\u671f\u548c\u65f6\u95f4(\u53f3\u4e0a\u89d2)\u5c06\u6309\u8be5\u95f4\u9694\u81ea\u884c\u66f4\u65b0\u3002\u7528\u6237\u5217\u8868\u5c06\u968f\u7740\u7528\u6237\u767b\u5f55\u548c\u6ce8\u9500\u800c\u589e\u957f\u548c\u7f29\u5c0f\u3002<\/p>\n

\r\n$ watch who\r\n<\/pre>\n

\u6b64\u547d\u4ee4\u5c06\u663e\u793a\u5982\u4e0b\u6240\u793a\u7684\u767b\u5f55\u5217\u8868\uff1a<\/p>\n

\r\n    Every 2.0s: who                              dragonfly: Thu Feb 27 10:52:00 2020\r\n    nemo     pts\/0        2020-02-27 08:07 (192.168.0.11)\r\n    shs      pts\/1        2020-02-27 10:58 (192.168.0.5)<\/pre>\n

\u4f60\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0 -n \u9009\u9879(\u4f8b\u5982 -n 10)\u6765\u4fee\u6539\u66f4\u65b0\u95f4\u7684\u4e0d\u540c\u79d2\u6570\uff0c\u4ee5\u4fee\u6539\u66f4\u65b0\u95f4\u9694\uff0c\u4ece\u800c\u83b7\u53d6\u8f83\u5c11\u7684\u66f4\u65b0\u9891\u7387\u3002<\/p>\n

\r\n$ watch -n 10 who<\/pre>\n

\u4e0a\u8ff0\u547d\u4ee4\u5c06\u4ee5\u65b0\u7684\u95f4\u9694\u663e\u793a\uff0c\u5e76\u4e14\u663e\u793a\u7684\u65f6\u95f4\u66f4\u65b0\u9891\u7387\u8f83\u4f4e\uff0c\u4ece\u800c\u4f7f\u663e\u793a\u65f6\u95f4\u4e0e\u6240\u9009\u95f4\u9694\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n

\r\nEvery 10.0s: who                             dragonfly: Thu Feb 27 11:05:47 2020\r\n    nemo     pts\/0        2020-02-27 08:07 (192.168.0.11)\r\n    shs      pts\/1        2020-02-27 10:58 (192.168.0.5)<\/pre>\n

\u5982\u679c\u4f60\u5e0c\u671b\u4ec5\u67e5\u770b\u547d\u4ee4\u7684\u8f93\u51fa\uff0c\u800c\u4e0d\u662f\u6807\u9898(\u524d 2 \u884c)\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0 -t(\u65e0\u6807\u9898)\u9009\u9879\u6765\u7701\u7565\u8fd9\u4e9b\u884c\u3002<\/p>\n

\r\n$ watch -t who\r\n<\/pre>\n

\u7136\u540e\uff0c\u4f60\u7684\u5c4f\u5e55\u5c06\u663e\u793a\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\n    nemo     pts\/0        2020-02-27 08:07 (192.168.0.11)\r\n    shs      pts\/1        2020-02-27 10:58 (192.168.0.5)\r\n<\/pre>\n

\u5982\u679c\u6bcf\u6b21\u8fd0\u884c\u76d1\u89c6\u7684\u547d\u4ee4\u65f6\uff0c\u8f93\u51fa\u90fd\u662f\u76f8\u540c\u7684\uff0c\u5219\u53ea\u6709\u6807\u9898\u884c(\u5982\u679c\u672a\u7701\u7565)\u4f1a\u66f4\u6539\u3002\u5176\u4f59\u663e\u793a\u7684\u4fe1\u606f\u5c06\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n

\u5982\u679c\u4f60\u5e0c\u671b watch \u547d\u4ee4\u5728\u5b83\u6b63\u5728\u76d1\u89c6\u7684\u547d\u4ee4\u7684\u8f93\u51fa\u53d1\u751f\u66f4\u65b0\u540e\u7acb\u5373\u9000\u51fa\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 -g(\u5c06\u5176\u89c6\u4e3a\u201c\u79bb\u5f00go away\u201d)\u9009\u9879\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u53ea\u662f\u5728\u7b49\u5f85\u5176\u4ed6\u4eba\u5f00\u59cb\u767b\u5f55\u7cfb\u7edf\uff0c\u5219\u53ef\u4ee5\u9009\u62e9\u6267\u884c\u6b64\u64cd\u4f5c\u3002<\/p>\n

\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 -d(\u5dee\u5f02differences)\u9009\u9879\u7a81\u51fa\u663e\u793a\u663e\u793a\u8f93\u51fa\u4e2d\u7684\u66f4\u6539\u3002\u7a81\u51fa\u663e\u793a\u53ea\u4f1a\u6301\u7eed\u4e00\u4e2a\u95f4\u9694(\u9ed8\u8ba4\u4e3a 2 \u79d2)\uff0c\u4f46\u6709\u52a9\u4e8e\u5f15\u8d77\u4f60\u5bf9\u66f4\u65b0\u7684\u6ce8\u610f\u3002<\/p>\n

\u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u4f7f\u7528 watch \u547d\u4ee4\u663e\u793a\u6b63\u5728\u4fa6\u542c\u8fde\u63a5\u7684\u670d\u52a1\u53ca\u5176\u4f7f\u7528\u7684\u7aef\u53e3\u3002\u867d\u7136\u8f93\u51fa\u4e0d\u592a\u53ef\u80fd\u66f4\u6539\uff0c\u4f46\u5b83\u4f1a\u63d0\u9192\u4f60\u4efb\u4f55\u65b0\u670d\u52a1\u6b63\u5728\u542f\u52a8\u6216\u5173\u95ed\u3002<\/p>\n

\r\n$ watch 'sudo lsof -i -P -n | grep LISTEN'\r\n<\/pre>\n

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6b63\u5728\u8fd0\u884c\u7684\u547d\u4ee4\u9700\u8981\u7528\u5f15\u53f7\u6269\u8d77\u6765\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u5c06 watch \u547d\u4ee4\u7684\u8f93\u51fa\u53d1\u9001\u5230 grep \u547d\u4ee4\u3002<\/p>\n

\u4f7f\u7528 watch -h \u547d\u4ee4\u5c06\u4e3a\u4f60\u63d0\u4f9b\u547d\u4ee4\u9009\u9879\u7684\u5217\u8868\u3002<\/p>\n

\r\n $ watch -h\r\n    Usage:\r\n     watch [options] command\r\n    Options:\r\n      -b, --beep             beep if command has a non-zero exit\r\n      -c, --color            interpret ANSI color and style sequences\r\n      -d, --differences[=]\r\n                             highlight changes between updates\r\n      -e, --errexit          exit if command has a non-zero exit\r\n      -g, --chgexit          exit when output from command changes\r\n      -n, --interval   seconds to wait between updates\r\n      -p, --precise          attempt run command in precise intervals\r\n      -t, --no-title         turn off header\r\n      -x, --exec             pass command to exec instead of \"sh -c\"\r\n     -h, --help     display this help and exit\r\n     -v, --version  output version information and exit<\/secs><\/permanent><\/pre>\n
\u4f7f\u7528 tail -f<\/strong><\/div>\n

tail -f \u547d\u4ee4\u4e0e watch \u6709\u4e00\u4e9b\u76f8\u540c\u4e4b\u5904\u3002\u5b83\u4e5f\u4f1a\u5728\u6dfb\u52a0\u6587\u4ef6\u65f6\u663e\u793a\u6587\u4ef6\u7684\u5e95\u90e8\u548c\u5176\u4ed6\u5185\u5bb9\u3002\u4f60\u4e0d\u5fc5\u4e00\u6b21\u53c8\u4e00\u6b21\u5730\u8fd0\u884c tail \u547d\u4ee4\uff0c\u800c\u662f\u8fd0\u884c\u4e00\u4e2a\u547d\u4ee4\u5e76\u83b7\u5f97\u53ef\u91cd\u590d\u66f4\u65b0\u663e\u793a\u89c6\u56fe\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u65e5\u5fd7\uff1a<\/p>\n

\r\n$ tail -f \/var\/log\/syslog\r\n<\/pre>\n

\u67d0\u4e9b\u6587\u4ef6(\u5982 \/var\/log\/wtmp)\u4e0d\u9002\u5408\u8fd9\u79cd\u7c7b\u578b\u7684\u5904\u7406\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u683c\u5f0f\u4e0d\u662f\u666e\u901a\u6587\u672c\u6587\u4ef6\uff0c\u4f46\u662f\u901a\u8fc7\u7ec4\u5408 watch \u548c tail\uff0c\u4f60\u53ef\u4ee5\u83b7\u5f97\u7c7b\u4f3c\u7684\u7ed3\u679c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\nwatch 'who \/var\/log\/wtmp | tail -20'\r\n<\/pre>\n

\u65e0\u8bba\u6709\u591a\u5c11\u7528\u6237\u4ecd\u5904\u4e8e\u767b\u5f55\u72b6\u6001\uff0c\u6b64\u547d\u4ee4\u90fd\u5c06\u53ea\u663e\u793a\u6700\u8fd1\u7684 5 \u6b21\u767b\u5f55\u3002\u5982\u679c\u53d1\u751f\u5176\u4ed6\u767b\u5f55\uff0c\u663e\u793a\u7ed3\u679c\u5c06\u6dfb\u52a0\u4e00\u884c\u8bb0\u5f55\u5e76\u5220\u9664\u9876\u884c\u8bb0\u5f55\u3002<\/p>\n

\r\nEvery 60.0s: who \/var\/log\/wtmp | tail -5    dragonfly: Thu Feb 27 12:46:07 2020\r\n    shs      pts\/0        2020-02-27 08:07 (192.168.0.5)\r\n    nemo     pts\/1        2020-02-27 08:26 (192.168.0.5)\r\n    shs      pts\/1        2020-02-27 10:58 (192.168.0.5)\r\n    nemo     pts\/1        2020-02-27 11:34 (192.168.0.5)\r\n    dory     pts\/1        2020-02-27 12:14 (192.168.0.5)<\/pre>\n

\u5bf9\u4f60\u6709\u65f6\u53ef\u80fd\u60f3\u8981\u76d1\u89c6\u7684\u4fe1\u606f\uff0c\u65e0\u8bba\u76d1\u89c6\u8fdb\u7a0b\u3001\u767b\u5f55\u540d\u8fd8\u662f\u7cfb\u7edf\u8d44\u6e90\uff0cwatch \u548c tail -f \u547d\u4ee4\u90fd\u53ef\u4ee5\u63d0\u4f9b\u81ea\u52a8\u66f4\u65b0\u89c6\u56fe\uff0c\u4ece\u800c\u4f7f\u76d1\u89c6\u4efb\u52a1\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

watch \u548c tail \u547d\u4ee4\u53ef\u4ee5\u5e2e\u52a9\u76d1\u89c6 Linux \u7cfb\u7edf\u4e0a\u7684\u6d3b\u52a8\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u8fd9\u4e24\u4e2a\u547d\u4ee4\u7684\u4e00\u4e9b\u6709\u7528\u7684\u4f7f\u7528\u65b9\u6cd5 […]<\/p>\n","protected":false},"author":317,"featured_media":232800,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-242169","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\/242169","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\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=242169"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/242169\/revisions"}],"predecessor-version":[{"id":242173,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/242169\/revisions\/242173"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/232800"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=242169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=242169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=242169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}