{"id":214013,"date":"2021-03-27T08:02:31","date_gmt":"2021-03-27T00:02:31","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=214013"},"modified":"2021-03-22T10:03:13","modified_gmt":"2021-03-22T02:03:13","slug":"linux-killall","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-killall.html","title":{"rendered":"\u5982\u4f55\u5728 Linux \u4e0a\u4f7f\u7528 kill \u548c killall \u6765\u7ba1\u7406\u8fdb\u7a0b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 ps\u3001kill \u548c killall \u547d\u4ee4\u6765\u7ec8\u6b62\u8fdb\u7a0b\u5e76\u56de\u6536\u7cfb\u7edf\u8d44\u6e90\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728 Linux \u4e2d\uff0c\u6bcf\u4e2a\u7a0b\u5e8f\u548c\u5b88\u62a4\u7a0b\u5e8fdaemon\u90fd\u662f\u4e00\u4e2a\u201c\u8fdb\u7a0bprocess\u201d\u3002 \u5927\u591a\u6570\u8fdb\u7a0b\u4ee3\u8868\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002\u800c\u53e6\u5916\u4e00\u4e9b\u7a0b\u5e8f\u53ef\u4ee5\u6d3e\u751f\u51fa\u5176\u4ed6\u8fdb\u7a0b\uff0c\u6bd4\u5982\u8bf4\u5b83\u4f1a\u4fa6\u542c\u67d0\u4e9b\u4e8b\u4ef6\u7684\u53d1\u751f\uff0c\u7136\u540e\u5bf9\u5176\u505a\u51fa\u54cd\u5e94\u3002\u5e76\u4e14\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u9700\u8981\u4e00\u5b9a\u7684\u5185\u5b58\u548c\u5904\u7406\u80fd\u529b\u3002\u4f60\u8fd0\u884c\u7684\u8fdb\u7a0b\u8d8a\u591a\uff0c\u6240\u9700\u7684\u5185\u5b58\u548c CPU \u4f7f\u7528\u5468\u671f\u5c31\u8d8a\u591a\u3002\u5728\u8001\u5f0f\u7535\u8111\uff08\u4f8b\u5982\u6211\u4f7f\u7528\u4e86 7 \u5e74\u7684\u7b14\u8bb0\u672c\u7535\u8111\uff09\u6216\u8f7b\u91cf\u7ea7\u8ba1\u7b97\u673a\uff08\u4f8b\u5982\u6811\u8393\u6d3e\uff09\u4e0a\uff0c\u5982\u679c\u4f60\u5173\u6ce8\u8fc7\u540e\u53f0\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u5c31\u80fd\u5145\u5206\u5229\u7528\u4f60\u7684\u7cfb\u7edf\u3002<\/p>\n

\"\"<\/p>\n

\u4f60\u53ef\u4ee5\u4f7f\u7528 ps \u547d\u4ee4\u6765\u67e5\u770b\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\u3002\u4f60\u901a\u5e38\u4f1a\u4f7f\u7528 ps \u547d\u4ee4\u7684\u53c2\u6570\u6765\u663e\u793a\u51fa\u66f4\u591a\u7684\u8f93\u51fa\u4fe1\u606f\u3002\u6211\u559c\u6b22\u4f7f\u7528 -e \u53c2\u6570\u6765\u67e5\u770b\u6bcf\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u4ee5\u53ca -f \u53c2\u6570\u6765\u83b7\u5f97\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u5168\u90e8\u7ec6\u8282\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n

\r\n$ ps\r\n    PID TTY          TIME CMD\r\n  88000 pts\/0    00:00:00 bash\r\n  88052 pts\/0    00:00:00 ps\r\n  88053 pts\/0    00:00:00 head\r\n<\/pre>\n
\r\n$ ps -e | head\r\n    PID TTY          TIME CMD\r\n      1 ?        00:00:50 systemd\r\n      2 ?        00:00:00 kthreadd\r\n      3 ?        00:00:00 rcu_gp\r\n      4 ?        00:00:00 rcu_par_gp\r\n      6 ?        00:00:02 kworker\/0:0H-events_highpri\r\n      9 ?        00:00:00 mm_percpu_wq\r\n     10 ?        00:00:01 ksoftirqd\/0\r\n     11 ?        00:00:12 rcu_sched\r\n     12 ?        00:00:00 migration\/0\r\n<\/pre>\n
\r\n$ ps -ef | head\r\nUID          PID    PPID  C STIME TTY          TIME CMD\r\nroot           1       0  0 13:51 ?        00:00:50 \/usr\/lib\/systemd\/systemd --switched-root --system --deserialize 36\r\nroot           2       0  0 13:51 ?        00:00:00 [kthreadd]\r\nroot           3       2  0 13:51 ?        00:00:00 [rcu_gp]\r\nroot           4       2  0 13:51 ?        00:00:00 [rcu_par_gp]\r\nroot           6       2  0 13:51 ?        00:00:02 [kworker\/0:0H-kblockd]\r\nroot           9       2  0 13:51 ?        00:00:00 [mm_percpu_wq]\r\nroot          10       2  0 13:51 ?        00:00:01 [ksoftirqd\/0]\r\nroot          11       2  0 13:51 ?        00:00:12 [rcu_sched]\r\nroot          12       2  0 13:51 ?        00:00:00 [migration\/0]\r\n<\/pre>\n

\u6700\u540e\u7684\u4f8b\u5b50\u663e\u793a\u6700\u591a\u7684\u7ec6\u8282\u3002\u5728\u6bcf\u4e00\u884c\uff0cUID\uff08\u7528\u6237 ID\uff09\u663e\u793a\u4e86\u8be5\u8fdb\u7a0b\u7684\u6240\u6709\u8005\u3002PID\uff08\u8fdb\u7a0b ID\uff09\u4ee3\u8868\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u6570\u5b57 ID\uff0c\u800c PPID\uff08\u7236\u8fdb\u7a0b ID\uff09\u8868\u793a\u5176\u7236\u8fdb\u7a0b\u7684\u6570\u5b57 ID\u3002\u5728\u4efb\u4f55 Unix \u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u662f\u4ece 1 \u5f00\u59cb\u7f16\u53f7\uff0c\u662f\u5185\u6838\u542f\u52a8\u540e\u8fd0\u884c\u7684\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\u3002\u5728\u8fd9\u91cc\uff0csystemd \u662f\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5b83\u50ac\u751f\u4e86 kthreadd\uff0c\u800c kthreadd \u8fd8\u521b\u5efa\u4e86\u5176\u4ed6\u8fdb\u7a0b\uff0c\u5305\u62ec rcu_gp\u3001rcu_par_gp \u7b49\u4e00\u7cfb\u5217\u8fdb\u7a0b\u3002<\/p>\n

\u4f7f\u7528 kill \u547d\u4ee4\u6765\u7ba1\u7406\u8fdb\u7a0b<\/strong><\/div>\n

\u7cfb\u7edf\u4f1a\u5904\u7406\u5927\u591a\u6570\u540e\u53f0\u8fdb\u7a0b\uff0c\u6240\u4ee5\u4f60\u4e0d\u9700\u8981\u64cd\u5fc3\u8fd9\u4e9b\u8fdb\u7a0b\u3002\u4f60\u53ea\u9700\u8981\u5173\u6ce8\u90a3\u4e9b\u4f60\u6240\u8fd0\u884c\u7684\u5e94\u7528\u521b\u5efa\u7684\u8fdb\u7a0b\u3002\u867d\u7136\u8bb8\u591a\u5e94\u7528\u4e00\u6b21\u53ea\u8fd0\u884c\u4e00\u4e2a\u8fdb\u7a0b\uff08\u5982\u97f3\u4e50\u64ad\u653e\u5668\u3001\u7ec8\u7aef\u6a21\u62df\u5668\u6216\u6e38\u620f\u7b49\uff09\uff0c\u4f46\u5176\u4ed6\u5e94\u7528\u5219\u53ef\u80fd\u521b\u5efa\u540e\u53f0\u8fdb\u7a0b\u3002\u5176\u4e2d\u4e00\u4e9b\u5e94\u7528\u53ef\u80fd\u5f53\u4f60\u9000\u51fa\u540e\u8fd8\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u4ee5\u4fbf\u4e0b\u6b21\u4f60\u4f7f\u7528\u7684\u65f6\u5019\u80fd\u5feb\u901f\u542f\u52a8\u3002<\/p>\n

\u5f53\u6211\u8fd0\u884c Chromium\uff08\u4f5c\u4e3a\u8c37\u6b4c Chrome \u6d4f\u89c8\u5668\u6240\u57fa\u4e8e\u7684\u5f00\u6e90\u9879\u76ee\uff09\u65f6\uff0c\u8fdb\u7a0b\u7ba1\u7406\u4fbf\u6210\u4e86\u95ee\u9898\u3002 Chromium \u5728\u6211\u7684\u7b14\u8bb0\u672c\u7535\u8111\u4e0a\u8fd0\u884c\u975e\u5e38\u5403\u529b\uff0c\u5e76\u4ea7\u751f\u4e86\u8bb8\u591a\u989d\u5916\u7684\u8fdb\u7a0b\u3002\u73b0\u5728\u6211\u4ec5\u6253\u5f00\u4e94\u4e2a\u9009\u9879\u5361\uff0c\u5c31\u80fd\u770b\u5230\u8fd9\u4e9b Chromium \u8fdb\u7a0b\uff1a<\/p>\n

\r\n$ ps -ef | fgrep chromium\r\njhall      66221   [...]  \/usr\/lib64\/chromium-browser\/chromium-browser [...]\r\njhall      66230   [...]  \/usr\/lib64\/chromium-browser\/chromium-browser [...]\r\n[...]\r\njhall      66861   [...]  \/usr\/lib64\/chromium-browser\/chromium-browser [...]\r\njhall      67329   65132  0 15:45 pts\/0    00:00:00 grep -F chromium\r\n<\/pre>\n

\u6211\u5df2\u7ecf\u7701\u7565\u4e00\u4e9b\u884c\uff0c\u5176\u4e2d\u6709 20 \u4e2a Chromium \u8fdb\u7a0b\u548c\u4e00\u4e2a\u6b63\u5728\u641c\u7d22 \u201cchromium\" \u5b57\u7b26\u7684 grep \u8fdb\u7a0b\u3002<\/p>\n

\r\n$ ps -ef | fgrep chromium | wc -l\r\n21\r\n<\/pre>\n

\u4f46\u662f\u5728\u6211\u9000\u51fa Chromium \u4e4b\u540e\uff0c\u8fd9\u4e9b\u8fdb\u7a0b\u4ecd\u65e7\u8fd0\u884c\u3002\u5982\u4f55\u5173\u95ed\u5b83\u4eec\u5e76\u56de\u6536\u8fd9\u4e9b\u8fdb\u7a0b\u5360\u7528\u7684\u5185\u5b58\u548c CPU \u5462\uff1f<\/p>\n

kill \u547d\u4ee4\u80fd\u8ba9\u4f60\u7ec8\u6b62\u4e00\u4e2a\u8fdb\u7a0b\u3002\u5728\u6700\u7b80\u5355\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u544a\u8bc9 kill \u547d\u4ee4\u7ec8\u6b62\u4f60\u60f3\u7ec8\u6b62\u7684\u8fdb\u7a0b\u7684 PID\u3002\u4f8b\u5982\uff0c\u8981\u7ec8\u6b62\u8fd9\u4e9b\u8fdb\u7a0b\uff0c\u6211\u9700\u8981\u5bf9 20 \u4e2a Chromium \u8fdb\u7a0b ID \u90fd\u6267\u884c kill \u547d\u4ee4\u3002\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u547d\u4ee4\u884c\u83b7\u53d6 Chromium \u7684 PID\uff0c\u800c\u53e6\u4e00\u79cd\u65b9\u6cd5\u9488\u5bf9\u8be5\u5217\u8868\u8fd0\u884c kill\uff1a<\/p>\n

\r\n$ ps -ef | fgrep \/usr\/lib64\/chromium-browser\/chromium-browser | awk '{print $2}'\r\n66221\r\n66230\r\n66239\r\n66257\r\n66262\r\n66283\r\n66284\r\n66285\r\n66324\r\n66337\r\n66360\r\n66370\r\n66386\r\n66402\r\n66503\r\n66539\r\n66595\r\n66734\r\n66848\r\n66861\r\n69702\r\n \r\n$ ps -ef | fgrep \/usr\/lib64\/chromium-browser\/chromium-browser | awk '{print $2}' > \/tmp\/pids\r\n$ kill $(cat \/tmp\/pids)\r\n<\/pre>\n

\u6700\u540e\u4e24\u884c\u662f\u5173\u952e\u3002\u7b2c\u4e00\u4e2a\u547d\u4ee4\u884c\u4e3a Chromium \u6d4f\u89c8\u5668\u751f\u6210\u4e00\u4e2a\u8fdb\u7a0b ID \u5217\u8868\u3002\u7b2c\u4e8c\u4e2a\u547d\u4ee4\u884c\u9488\u5bf9\u8be5\u8fdb\u7a0b ID \u5217\u8868\u8fd0\u884c kill \u547d\u4ee4\u3002<\/p>\n

\u4ecb\u7ecd killall \u547d\u4ee4<\/strong><\/div>\n

\u4e00\u6b21\u7ec8\u6b62\u591a\u4e2a\u8fdb\u7a0b\u6709\u4e2a\u66f4\u7b80\u5355\u65b9\u6cd5\uff0c\u4f7f\u7528 killall \u547d\u4ee4\u3002\u4f60\u6216\u8bb8\u53ef\u4ee5\u6839\u636e\u540d\u79f0\u731c\u6d4b\u51fa\uff0ckillall \u4f1a\u7ec8\u6b62\u6240\u6709\u4e0e\u8be5\u540d\u5b57\u5339\u914d\u7684\u8fdb\u7a0b\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u547d\u4ee4\u6765\u505c\u6b62\u6240\u6709\u6d41\u6c13 Chromium \u8fdb\u7a0b\u3002\u8fd9\u5f88\u7b80\u5355\uff1a<\/p>\n

\r\n$ killall \/usr\/lib64\/chromium-browser\/chromium-browser\r\n<\/pre>\n

\u4f46\u662f\u8981\u5c0f\u5fc3\u4f7f\u7528 killall\u3002\u8be5\u547d\u4ee4\u80fd\u591f\u7ec8\u6b62\u4e0e\u4f60\u6240\u7ed9\u51fa\u540d\u79f0\u76f8\u5339\u914d\u7684\u6240\u6709\u8fdb\u7a0b\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6211\u559c\u6b22\u5148\u4f7f\u7528 ps -ef \u547d\u4ee4\u6765\u68c0\u67e5\u6211\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u7136\u540e\u9488\u5bf9\u8981\u505c\u6b62\u7684\u547d\u4ee4\u7684\u51c6\u786e\u8def\u5f84\u8fd0\u884c killall\u3002<\/p>\n

\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 -i \u6216 --interactive \u53c2\u6570\uff0c\u6765\u8ba9 killkill \u5728\u505c\u6b62\u6bcf\u4e2a\u8fdb\u7a0b\u4e4b\u524d\u63d0\u793a\u4f60\u3002<\/p>\n

killall \u8fd8\u652f\u6301\u4f7f\u7528 -o \u6216 --older-than \u53c2\u6570\u6765\u67e5\u627e\u6bd4\u7279\u5b9a\u65f6\u95f4\u66f4\u65e9\u7684\u8fdb\u7a0b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u53d1\u73b0\u4e86\u4e00\u7ec4\u5df2\u7ecf\u8fd0\u884c\u4e86\u597d\u51e0\u5929\u7684\u6076\u610f\u8fdb\u7a0b\uff0c\u8fd9\u5c06\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u53c8\u6216\u662f\uff0c\u4f60\u53ef\u4ee5\u67e5\u627e\u6bd4\u7279\u5b9a\u65f6\u95f4\u66f4\u665a\u7684\u8fdb\u7a0b\uff0c\u4f8b\u5982\u4f60\u6700\u8fd1\u542f\u52a8\u7684\u5931\u63a7\u8fdb\u7a0b\u3002\u4f7f\u7528 -y \u6216 --young-than \u53c2\u6570\u6765\u67e5\u627e\u8fd9\u4e9b\u8fdb\u7a0b\u3002<\/p>\n

\u5176\u4ed6\u7ba1\u7406\u8fdb\u7a0b\u7684\u65b9\u5f0f<\/strong><\/div>\n

\u8fdb\u7a0b\u7ba1\u7406\u662f\u7cfb\u7edf\u7ef4\u62a4\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u5728\u6211\u4f5c\u4e3a Unix \u548c Linux \u7cfb\u7edf\u7ba1\u7406\u5458\u7684\u65e9\u671f\u804c\u4e1a\u751f\u6daf\u4e2d\uff0c\u6740\u6b7b\u975e\u6cd5\u4f5c\u4e1a\u7684\u80fd\u529b\u662f\u4fdd\u6301\u7cfb\u7edf\u6b63\u5e38\u8fd0\u884c\u7684\u5173\u952e\u3002\u5728\u5982\u4eca\uff0c\u4f60\u53ef\u80fd\u4e0d\u9700\u8981\u4eb2\u624b\u5728 Linux \u4e0a\u7684\u7ec8\u6b62\u6d41\u6c13\u8fdb\u7a0b\uff0c\u4f46\u662f\u77e5\u9053 kill \u548c killall \u80fd\u591f\u5728\u6700\u7ec8\u51fa\u73b0\u95ee\u9898\u65f6\u4e3a\u4f60\u63d0\u4f9b\u5e2e\u52a9\u3002<\/p>\n

\u4f60\u4e5f\u80fd\u5bfb\u627e\u5176\u4ed6\u65b9\u5f0f\u6765\u7ba1\u7406\u8fdb\u7a0b\u3002\u5728\u6211\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u6211\u5e76\u4e0d\u9700\u8981\u5728\u6211\u9000\u51fa\u6d4f\u89c8\u5668\u540e\uff0c\u4f7f\u7528 kill \u6216 killall \u6765\u7ec8\u6b62\u540e\u53f0 Chromium \u8fdb\u7a0b\u3002\u5728 Chromium \u4e2d\u6709\u4e2a\u7b80\u5355\u8bbe\u7f6e\u5c31\u53ef\u4ee5\u8fdb\u884c\u63a7\u5236\uff1a
\n\"\"<\/p>\n

\u4e0d\u8fc7\uff0c\u59cb\u7ec8\u5173\u6ce8\u7cfb\u7edf\u4e0a\u6b63\u5728\u8fd0\u884c\u54ea\u4e9b\u8fdb\u7a0b\uff0c\u5e76\u4e14\u5728\u9700\u8981\u7684\u65f6\u5019\u8fdb\u884c\u5e72\u9884\u662f\u4e00\u4e2a\u660e\u667a\u4e4b\u4e3e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728 Linux \u4e2d\uff0c\u6bcf\u4e2a\u7a0b\u5e8f\u548c\u5b88\u62a4\u7a0b\u5e8fdaemon\u90fd\u662f\u4e00\u4e2a\u201c\u8fdb\u7a0bprocess\u201d\u3002 \u5927\u591a\u6570\u8fdb\u7a0b\u4ee3\u8868\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c […]<\/p>\n","protected":false},"author":370,"featured_media":192404,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-214013","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\/214013","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\/370"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=214013"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/214013\/revisions"}],"predecessor-version":[{"id":214017,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/214013\/revisions\/214017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/192404"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=214013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=214013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=214013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}