{"id":8287,"date":"2022-08-03T09:09:41","date_gmt":"2022-08-03T01:09:41","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=8287"},"modified":"2022-08-03T09:23:45","modified_gmt":"2022-08-03T01:23:45","slug":"process-run-in-background","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/process-run-in-background.html","title":{"rendered":"Linux \u6280\u5de7\uff1a\u8ba9\u8fdb\u7a0b\u5728\u540e\u53f0\u53ef\u9760\u8fd0\u884c\u7684\u51e0\u79cd\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u60f3\u8ba9\u8fdb\u7a0b\u5728\u65ad\u5f00\u8fde\u63a5\u540e\u4f9d\u7136\u4fdd\u6301\u8fd0\u884c\uff1f\u5982\u679c\u8be5\u8fdb\u7a0b\u5df2\u7ecf\u5f00\u59cb\u8fd0\u884c\u4e86\u8be5\u5982\u4f55\u8865\u6551\uff1f \u5982\u679c\u6709\u5927\u91cf\u8fd9\u7c7b\u9700\u6c42\u5982\u4f55\u7b80\u5316\u64cd\u4f5c\uff1f<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6211\u4eec\u7ecf\u5e38\u4f1a\u78b0\u5230\u8fd9\u6837\u7684\u95ee\u9898\uff0c\u7528 telnet\/ssh \u767b\u5f55\u4e86\u8fdc\u7a0b\u7684 Linux \u670d\u52a1\u5668\uff0c\u8fd0\u884c\u4e86\u4e00\u4e9b\u8017\u65f6\u8f83\u957f\u7684\u4efb\u52a1\uff0c \u7ed3\u679c\u5374\u7531\u4e8e\u7f51\u7edc\u7684\u4e0d\u7a33\u5b9a\u5bfc\u81f4\u4efb\u52a1\u4e2d\u9014\u5931\u8d25\u3002\u5982\u4f55\u8ba9\u547d\u4ee4\u63d0\u4ea4\u540e\u4e0d\u53d7\u672c\u5730\u5173\u95ed\u7ec8\u7aef\u7a97\u53e3\/\u7f51\u7edc\u65ad\u5f00\u8fde\u63a5\u7684\u5e72\u6270\u5462\uff1f<\/p>\n

\u4e0b\u9762\u4e3e\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c \u60a8\u53ef\u4ee5\u9488\u5bf9\u4e0d\u540c\u7684\u573a\u666f\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n

nohup\/setsid\/&<\/strong><\/div>\n
\u573a\u666f\uff1a<\/strong><\/span><\/div>\n

\u5982\u679c\u53ea\u662f\u4e34\u65f6\u6709\u4e00\u4e2a\u547d\u4ee4\u9700\u8981\u957f\u65f6\u95f4\u8fd0\u884c\uff0c\u4ec0\u4e48\u65b9\u6cd5\u80fd\u6700\u7b80\u4fbf\u7684\u4fdd\u8bc1\u5b83\u5728\u540e\u53f0\u7a33\u5b9a\u8fd0\u884c\u5462\uff1f<\/p>\n

\u6211\u4eec\u77e5\u9053\uff0c\u5f53\u7528\u6237\u6ce8\u9500\uff08logout\uff09\u6216\u8005\u7f51\u7edc\u65ad\u5f00\u65f6\uff0c\u7ec8\u7aef\u4f1a\u6536\u5230 HUP\uff08hangup\uff09\u4fe1\u53f7\u4ece\u800c\u5173\u95ed\u5176\u6240\u6709\u5b50\u8fdb\u7a0b\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u7684\u89e3\u51b3\u529e\u6cd5\u5c31\u6709\u4e24\u79cd\u9014\u5f84\uff1a\u8981\u4e48\u8ba9\u8fdb\u7a0b\u5ffd\u7565 HUP \u4fe1\u53f7\uff0c\u8981\u4e48\u8ba9\u8fdb\u7a0b\u8fd0\u884c\u5728\u65b0\u7684\u4f1a\u8bdd\u91cc\u4ece\u800c\u6210\u4e3a\u4e0d\u5c5e\u4e8e\u6b64\u7ec8\u7aef\u7684\u5b50\u8fdb\u7a0b\u3002<\/p>\n

1. nohup<\/strong><\/span><\/div>\n

nohup \u65e0\u7591\u662f\u6211\u4eec\u9996\u5148\u60f3\u5230\u7684\u529e\u6cd5\u3002\u987e\u540d\u601d\u4e49\uff0cnohup \u7684\u7528\u9014\u5c31\u662f\u8ba9\u63d0\u4ea4\u7684\u547d\u4ee4\u5ffd\u7565 hangup \u4fe1\u53f7\u3002\u8ba9\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b nohup \u7684\u5e2e\u52a9\u4fe1\u606f\uff1a<\/p>\n

NOHUP(1)                        User Commands                        NOHUP(1)\r\n\r\nNAME\r\n       nohup - run a command immune to hangups, with output to a non-tty\r\n\r\nSYNOPSIS\r\n       nohup COMMAND [ARG]...\r\n       nohup OPTION\r\n\r\nDESCRIPTION\r\n       Run COMMAND, ignoring hangup signals.\r\n\r\n       --help display this help and exit\r\n\r\n       --version\r\n              output version information and exit\r\n<\/pre>\n

\u53ef\u89c1\uff0cnohup \u7684\u4f7f\u7528\u662f\u5341\u5206\u65b9\u4fbf\u7684\uff0c\u53ea\u9700\u5728\u8981\u5904\u7406\u7684\u547d\u4ee4\u524d\u52a0\u4e0a nohup \u5373\u53ef\uff0c\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u7f3a\u7701\u4f1a\u88ab\u91cd\u5b9a\u5411\u5230 nohup.out \u6587\u4ef6\u4e2d\u3002\u4e00\u822c\u6211\u4eec\u53ef\u5728\u7ed3\u5c3e\u52a0\u4e0a\"&\"\u6765\u5c06\u547d\u4ee4\u540c\u65f6\u653e\u5165\u540e\u53f0\u8fd0\u884c\uff0c\u4e5f\u53ef\u7528\">filename 2>&1\"\u6765\u66f4\u6539\u7f3a\u7701\u7684\u91cd\u5b9a\u5411\u6587\u4ef6\u540d\u3002<\/p>\n

nohup \u793a\u4f8b<\/p>\n

[root@pvcent107 ~]# nohup ping www.ibm.com &\r\n[1] 3059\r\nnohup: appending output to `nohup.out'\r\n[root@pvcent107 ~]# ps -ef |grep 3059\r\nroot      3059   984  0 21:06 pts\/3    00:00:00 ping www.ibm.com\r\nroot      3067   984  0 21:06 pts\/3    00:00:00 grep 3059\r\n[root@pvcent107 ~]#\r\n<\/pre>\n
2.setsid<\/strong><\/span><\/div>\n

nohup \u65e0\u7591\u80fd\u901a\u8fc7\u5ffd\u7565 HUP \u4fe1\u53f7\u6765\u4f7f\u6211\u4eec\u7684\u8fdb\u7a0b\u907f\u514d\u4e2d\u9014\u88ab\u4e2d\u65ad\uff0c\u4f46\u5982\u679c\u6211\u4eec\u6362\u4e2a\u89d2\u5ea6\u601d\u8003\uff0c\u5982\u679c\u6211\u4eec\u7684\u8fdb\u7a0b\u4e0d\u5c5e\u4e8e\u63a5\u53d7 HUP \u4fe1\u53f7\u7684\u7ec8\u7aef\u7684\u5b50\u8fdb\u7a0b\uff0c\u90a3\u4e48\u81ea\u7136\u4e5f\u5c31\u4e0d\u4f1a\u53d7\u5230 HUP \u4fe1\u53f7\u7684\u5f71\u54cd\u4e86\u3002setsid \u5c31\u80fd\u5e2e\u52a9\u6211\u4eec\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u8ba9\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b setsid \u7684\u5e2e\u52a9\u4fe1\u606f\uff1a<\/p>\n

SETSID(8)                 Linux Programmer\u2019s Manual                 SETSID(8)\r\n\r\nNAME\r\n       setsid - run a program in a new session\r\n\r\nSYNOPSIS\r\n       setsid program [ arg ... ]\r\n\r\nDESCRIPTION\r\n       setsid runs a program in a new session.\r\n<\/pre>\n

\u53ef\u89c1 setsid \u7684\u4f7f\u7528\u4e5f\u662f\u975e\u5e38\u65b9\u4fbf\u7684\uff0c\u4e5f\u53ea\u9700\u5728\u8981\u5904\u7406\u7684\u547d\u4ee4\u524d\u52a0\u4e0a setsid \u5373\u53ef\u3002<\/p>\n

setsid \u793a\u4f8b<\/p>\n

[root@pvcent107 ~]# setsid ping www.ibm.com\r\n[root@pvcent107 ~]# ps -ef |grep www.ibm.com\r\nroot     31094     1  0 07:28 ?        00:00:00 ping www.ibm.com\r\nroot     31102 29217  0 07:29 pts\/4    00:00:00 grep www.ibm.com\r\n[root@pvcent107 ~]#\r\n<\/pre>\n

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e0a\u4f8b\u4e2d\u6211\u4eec\u7684\u8fdb\u7a0b ID(PID)\u4e3a31094\uff0c\u800c\u5b83\u7684\u7236 ID\uff08PPID\uff09\u4e3a1\uff08\u5373\u4e3a init \u8fdb\u7a0b ID\uff09\uff0c\u5e76\u4e0d\u662f\u5f53\u524d\u7ec8\u7aef\u7684\u8fdb\u7a0b ID\u3002\u8bf7\u5c06\u6b64\u4f8b\u4e0enohup \u4f8b\u4e2d\u7684\u7236 ID \u505a\u6bd4\u8f83\u3002<\/p>\n

3.&<\/strong><\/span><\/div>\n

\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u5173\u4e8e subshell \u7684\u5c0f\u6280\u5de7\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u547d\u540d\u5305\u542b\u5728\u201c()\u201d\u4e2d\u5c31\u80fd\u8ba9\u8fd9\u4e9b\u547d\u4ee4\u5728\u5b50 shell \u4e2d\u8fd0\u884c\u4e2d\uff0c\u4ece\u800c\u6269\u5c55\u51fa\u5f88\u591a\u6709\u8da3\u7684\u529f\u80fd\uff0c\u6211\u4eec\u73b0\u5728\u8981\u8ba8\u8bba\u7684\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\u3002<\/p>\n

\u5f53\u6211\u4eec\u5c06\"&\"\u4e5f\u653e\u5165\u201c()\u201d\u5185\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u4f1a\u53d1\u73b0\u6240\u63d0\u4ea4\u7684\u4f5c\u4e1a\u5e76\u4e0d\u5728\u4f5c\u4e1a\u5217\u8868\u4e2d\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u662f\u65e0\u6cd5\u901a\u8fc7jobs\u6765\u67e5\u770b\u7684\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e3a\u4ec0\u4e48\u8fd9\u6837\u5c31\u80fd\u8eb2\u8fc7 HUP \u4fe1\u53f7\u7684\u5f71\u54cd\u5427\u3002<\/p>\n

subshell \u793a\u4f8b<\/p>\n

[root@pvcent107 ~]# (ping www.ibm.com &)\r\n[root@pvcent107 ~]# ps -ef |grep www.ibm.com\r\nroot     16270     1  0 14:13 pts\/4    00:00:00 ping www.ibm.com\r\nroot     16278 15362  0 14:13 pts\/4    00:00:00 grep www.ibm.com\r\n[root@pvcent107 ~]#\r\n<\/pre>\n

\u4ece\u4e0a\u4f8b\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u65b0\u63d0\u4ea4\u7684\u8fdb\u7a0b\u7684\u7236 ID\uff08PPID\uff09\u4e3a1\uff08init \u8fdb\u7a0b\u7684 PID\uff09\uff0c\u5e76\u4e0d\u662f\u5f53\u524d\u7ec8\u7aef\u7684\u8fdb\u7a0b ID\u3002\u56e0\u6b64\u5e76\u4e0d\u5c5e\u4e8e\u5f53\u524d\u7ec8\u7aef\u7684\u5b50\u8fdb\u7a0b\uff0c\u4ece\u800c\u4e5f\u5c31\u4e0d\u4f1a\u53d7\u5230\u5f53\u524d\u7ec8\u7aef\u7684 HUP \u4fe1\u53f7\u7684\u5f71\u54cd\u4e86\u3002<\/p>\n

disown<\/strong><\/span><\/div>\n

\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\uff0c\u5982\u679c\u4e8b\u5148\u5728\u547d\u4ee4\u524d\u52a0\u4e0a nohup \u6216\u8005 setsid \u5c31\u53ef\u4ee5\u907f\u514d HUP \u4fe1\u53f7\u7684\u5f71\u54cd\u3002\u4f46\u662f\u5982\u679c\u6211\u4eec\u672a\u52a0\u4efb\u4f55\u5904\u7406\u5c31\u5df2\u7ecf\u63d0\u4ea4\u4e86\u547d\u4ee4\uff0c\u8be5\u5982\u4f55\u8865\u6551\u624d\u80fd\u8ba9\u5b83\u907f\u514d HUP \u4fe1\u53f7\u7684\u5f71\u54cd\u5462\uff1f<\/p>\n

\u8fd9\u65f6\u60f3\u52a0 nohup \u6216\u8005 setsid \u5df2\u7ecf\u4e3a\u65f6\u5df2\u665a\uff0c\u53ea\u80fd\u901a\u8fc7\u4f5c\u4e1a\u8c03\u5ea6\u548c disown \u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u4e86\u3002\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e0b disown \u7684\u5e2e\u52a9\u4fe1\u606f\uff1a<\/p>\n

disown [-ar] [-h] [jobspec ...]\r\n\tWithout options, each jobspec is  removed  from  the  table  of\r\n\tactive  jobs.   If  the -h option is given, each jobspec is not\r\n\tremoved from the table, but is marked so  that  SIGHUP  is  not\r\n\tsent  to the job if the shell receives a SIGHUP.  If no jobspec\r\n\tis present, and neither the -a nor the -r option  is  supplied,\r\n\tthe  current  job  is  used.  If no jobspec is supplied, the -a\r\n\toption means to remove or mark all jobs; the -r option  without\r\n\ta  jobspec  argument  restricts operation to running jobs.  The\r\n\treturn value is 0 unless a jobspec does  not  specify  a  valid\r\n\tjob.\r\n<\/pre>\n

\u53ef\u4ee5\u770b\u51fa\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5982\u4e0b\u65b9\u5f0f\u6765\u8fbe\u6210\u6211\u4eec\u7684\u76ee\u7684\u3002<\/p>\n

\u7075\u6d3b\u8fd0\u7528 CTRL-z<\/p>\n

\u5728\u6211\u4eec\u7684\u65e5\u5e38\u5de5\u4f5c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7528 CTRL-z \u6765\u5c06\u5f53\u524d\u8fdb\u7a0b\u6302\u8d77\u5230\u540e\u53f0\u6682\u505c\u8fd0\u884c\uff0c\u6267\u884c\u4e00\u4e9b\u522b\u7684\u64cd\u4f5c\uff0c\u7136\u540e\u518d\u7528 fg \u6765\u5c06\u6302\u8d77\u7684\u8fdb\u7a0b\u91cd\u65b0\u653e\u56de\u524d\u53f0\uff08\u4e5f\u53ef\u7528 bg \u6765\u5c06\u6302\u8d77\u7684\u8fdb\u7a0b\u653e\u5728\u540e\u53f0\uff09\u7ee7\u7eed\u8fd0\u884c\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u4e00\u4e2a\u7ec8\u7aef\u5185\u7075\u6d3b\u5207\u6362\u8fd0\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u8fd9\u4e00\u70b9\u5728\u8c03\u8bd5\u4ee3\u7801\u65f6\u5c24\u4e3a\u6709\u7528\u3002\u56e0\u4e3a\u5c06\u4ee3\u7801\u7f16\u8f91\u5668\u6302\u8d77\u5230\u540e\u53f0\u518d\u91cd\u65b0\u653e\u56de\u65f6\uff0c\u5149\u6807\u5b9a\u4f4d\u4ecd\u7136\u505c\u7559\u5728\u4e0a\u6b21\u6302\u8d77\u65f6\u7684\u4f4d\u7f6e\uff0c\u907f\u514d\u4e86\u91cd\u65b0\u5b9a\u4f4d\u7684\u9ebb\u70e6\u3002<\/p>\n

\u7528disown -h jobspec\u6765\u4f7f\u67d0\u4e2a\u4f5c\u4e1a\u5ffd\u7565HUP\u4fe1\u53f7\u3002\r\n\u7528disown -ah \u6765\u4f7f\u6240\u6709\u7684\u4f5c\u4e1a\u90fd\u5ffd\u7565HUP\u4fe1\u53f7\u3002\r\n\u7528disown -rh \u6765\u4f7f\u6b63\u5728\u8fd0\u884c\u7684\u4f5c\u4e1a\u5ffd\u7565HUP\u4fe1\u53f7\u3002\r\n<\/pre>\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u4f7f\u7528\u8fc7 disown \u4e4b\u540e\uff0c\u4f1a\u5c06\u628a\u76ee\u6807\u4f5c\u4e1a\u4ece\u4f5c\u4e1a\u5217\u8868\u4e2d\u79fb\u9664\uff0c\u6211\u4eec\u5c06\u4e0d\u80fd\u518d\u4f7f\u7528jobs\u6765\u67e5\u770b\u5b83\uff0c\u4f46\u662f\u4f9d\u7136\u80fd\u591f\u7528ps -ef\u67e5\u627e\u5230\u5b83\u3002<\/p>\n

\u4f46\u662f\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u64cd\u4f5c\u5bf9\u8c61\u662f\u4f5c\u4e1a\uff0c\u5982\u679c\u6211\u4eec\u5728\u8fd0\u884c\u547d\u4ee4\u65f6\u5728\u7ed3\u5c3e\u52a0\u4e86\"&\"\u6765\u4f7f\u5b83\u6210\u4e3a\u4e00\u4e2a\u4f5c\u4e1a\u5e76\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u90a3\u4e48\u5c31\u4e07\u4e8b\u5927\u5409\u4e86\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7jobs\u547d\u4ee4\u6765\u5f97\u5230\u6240\u6709\u4f5c\u4e1a\u7684\u5217\u8868\u3002\u4f46\u662f\u5982\u679c\u5e76\u6ca1\u6709\u628a\u5f53\u524d\u547d\u4ee4\u4f5c\u4e3a\u4f5c\u4e1a\u6765\u8fd0\u884c\uff0c\u5982\u4f55\u624d\u80fd\u5f97\u5230\u5b83\u7684\u4f5c\u4e1a\u53f7\u5462\uff1f\u7b54\u6848\u5c31\u662f\u7528 CTRL-z\uff08\u6309\u4f4fCtrl\u952e\u7684\u540c\u65f6\u6309\u4f4fz\u952e\uff09\u4e86\uff01
\nCTRL-z \u7684\u7528\u9014\u5c31\u662f\u5c06\u5f53\u524d\u8fdb\u7a0b\u6302\u8d77\uff08Suspend\uff09\uff0c\u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u7528jobs\u547d\u4ee4\u6765\u67e5\u8be2\u5b83\u7684\u4f5c\u4e1a\u53f7\uff0c\u518d\u7528bg jobspec\u6765\u5c06\u5b83\u653e\u5165\u540e\u53f0\u5e76\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u6302\u8d77\u4f1a\u5f71\u54cd\u5f53\u524d\u8fdb\u7a0b\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u8bf7\u614e\u7528\u6b64\u65b9\u6cd5\u3002<\/p>\n

disown \u793a\u4f8b1\uff08\u5982\u679c\u63d0\u4ea4\u547d\u4ee4\u65f6\u5df2\u7ecf\u7528\u201c&\u201d\u5c06\u547d\u4ee4\u653e\u5165\u540e\u53f0\u8fd0\u884c\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u201cdisown\u201d\uff09<\/p>\n

[root@pvcent107 build]# cp -r testLargeFile largeFile &\r\n[1] 4825\r\n[root@pvcent107 build]# jobs\r\n[1]+  Running                 cp -i -r testLargeFile largeFile &\r\n[root@pvcent107 build]# disown -h %1\r\n[root@pvcent107 build]# ps -ef |grep largeFile\r\nroot      4825   968  1 09:46 pts\/4    00:00:00 cp -i -r testLargeFile largeFile\r\nroot      4853   968  0 09:46 pts\/4    00:00:00 grep largeFile\r\n[root@pvcent107 build]# logout\r\n<\/pre>\n

disown \u793a\u4f8b2\uff08\u5982\u679c\u63d0\u4ea4\u547d\u4ee4\u65f6\u672a\u4f7f\u7528\u201c&\u201d\u5c06\u547d\u4ee4\u653e\u5165\u540e\u53f0\u8fd0\u884c\uff0c\u53ef\u4f7f\u7528 CTRL-z \u548c\u201cbg\u201d\u5c06\u5176\u653e\u5165\u540e\u53f0\uff0c\u518d\u4f7f\u7528\u201cdisown\u201d\uff09<\/p>\n

[root@pvcent107 build]# cp -r testLargeFile largeFile2\r\n\r\n[1]+  Stopped                 cp -i -r testLargeFile largeFile2\r\n[root@pvcent107 build]# bg %1\r\n[1]+ cp -i -r testLargeFile largeFile2 &\r\n[root@pvcent107 build]# jobs\r\n[1]+  Running                 cp -i -r testLargeFile largeFile2 &\r\n[root@pvcent107 build]# disown -h %1\r\n[root@pvcent107 build]# ps -ef |grep largeFile2\r\nroot      5790  5577  1 10:04 pts\/3    00:00:00 cp -i -r testLargeFile largeFile2\r\nroot      5824  5577  0 10:05 pts\/3    00:00:00 grep largeFile2\r\n[root@pvcent107 build]#\r\n<\/pre>\n
screen<\/strong><\/span><\/div>\n

\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86\u5982\u4f55\u8ba9\u8fdb\u7a0b\u514d\u53d7 HUP \u4fe1\u53f7\u7684\u5f71\u54cd\uff0c\u4f46\u662f\u5982\u679c\u6709\u5927\u91cf\u8fd9\u79cd\u547d\u4ee4\u9700\u8981\u5728\u7a33\u5b9a\u7684\u540e\u53f0\u91cc\u8fd0\u884c\uff0c\u5982\u4f55\u907f\u514d\u5bf9\u6bcf\u6761\u547d\u4ee4\u90fd\u505a\u8fd9\u6837\u7684\u64cd\u4f5c\u5462\uff1f<\/p>\n

\u6b64\u65f6\u6700\u65b9\u4fbf\u7684\u65b9\u6cd5\u5c31\u662f screen \u4e86\u3002\u7b80\u5355\u7684\u8bf4\uff0cscreen \u63d0\u4f9b\u4e86 ANSI\/VT100 \u7684\u7ec8\u7aef\u6a21\u62df\u5668\uff0c\u4f7f\u5b83\u80fd\u591f\u5728\u4e00\u4e2a\u771f\u5b9e\u7ec8\u7aef\u4e0b\u8fd0\u884c\u591a\u4e2a\u5168\u5c4f\u7684\u4f2a\u7ec8\u7aef\u3002screen \u7684\u53c2\u6570\u5f88\u591a\uff0c\u5177\u6709\u5f88\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u6211\u4eec\u5728\u6b64\u4ec5\u4ecb\u7ecd\u5176\u5e38\u7528\u529f\u80fd\u4ee5\u53ca\u7b80\u8981\u5206\u6790\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u4f7f\u7528 screen \u80fd\u591f\u907f\u514d HUP \u4fe1\u53f7\u7684\u5f71\u54cd\u3002\u6211\u4eec\u5148\u770b\u4e00\u4e0b screen \u7684\u5e2e\u52a9\u4fe1\u606f\uff1a<\/p>\n

SCREEN(1)                                                           SCREEN(1)\r\n\r\nNAME\r\n       screen - screen manager with VT100\/ANSI terminal emulation\r\n\r\nSYNOPSIS\r\n       screen [ -options ] [ cmd [ args ] ]\r\n       screen -r [[pid.]tty[.host]]\r\n       screen -r sessionowner\/[[pid.]tty[.host]]\r\n\r\nDESCRIPTION\r\n       Screen  is  a  full-screen  window manager that multiplexes a physical\r\n       terminal between several  processes  (typically  interactive  shells).\r\n       Each  virtual  terminal provides the functions of a DEC VT100 terminal\r\n       and, in addition, several control functions from the  ISO  6429  (ECMA\r\n       48,  ANSI  X3.64)  and ISO 2022 standards (e.g. insert\/delete line and\r\n       support for multiple character sets).  There is a  scrollback  history\r\n       buffer  for  each virtual terminal and a copy-and-paste mechanism that\r\n       allows moving text regions between windows.\r\n<\/pre>\n

\u4f7f\u7528 screen \u5f88\u65b9\u4fbf\uff0c\u6709\u4ee5\u4e0b\u51e0\u4e2a\u5e38\u7528\u9009\u9879\uff1a<\/p>\n

\u7528screen -dmS session name\u6765\u5efa\u7acb\u4e00\u4e2a\u5904\u4e8e\u65ad\u5f00\u6a21\u5f0f\u4e0b\u7684\u4f1a\u8bdd\uff08\u5e76\u6307\u5b9a\u5176\u4f1a\u8bdd\u540d\uff09\u3002\r\n\u7528screen -list \u6765\u5217\u51fa\u6240\u6709\u4f1a\u8bdd\u3002\r\n\u7528screen -r session name\u6765\u91cd\u65b0\u8fde\u63a5\u6307\u5b9a\u4f1a\u8bdd\u3002\r\n\u7528\u5feb\u6377\u952eCTRL-a d \u6765\u6682\u65f6\u65ad\u5f00\u5f53\u524d\u4f1a\u8bdd\u3002\r\n<\/pre>\n

screen \u793a\u4f8b<\/p>\n

[root@pvcent107 ~]# screen -dmS Urumchi\r\n[root@pvcent107 ~]# screen -list\r\nThere is a screen on:\r\n        12842.Urumchi   (Detached)\r\n1 Socket in \/tmp\/screens\/S-root.\r\n\r\n[root@pvcent107 ~]# screen -r Urumchi\r\n<\/pre>\n

\u5f53\u6211\u4eec\u7528\u201c-r\u201d\u8fde\u63a5\u5230 screen \u4f1a\u8bdd\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u8fd9\u4e2a\u4f2a\u7ec8\u7aef\u91cc\u9762\u4e3a\u6240\u6b32\u4e3a\uff0c\u518d\u4e5f\u4e0d\u7528\u62c5\u5fc3 HUP \u4fe1\u53f7\u4f1a\u5bf9\u6211\u4eec\u7684\u8fdb\u7a0b\u9020\u6210\u5f71\u54cd\uff0c\u4e5f\u4e0d\u7528\u7ed9\u6bcf\u4e2a\u547d\u4ee4\u524d\u90fd\u52a0\u4e0a\u201cnohup\u201d\u6216\u8005\u201csetsid\u201d\u4e86\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u8ba9\u6211\u6765\u770b\u4e00\u4e0b\u4e0b\u9762\u4e24\u4e2a\u4f8b\u5b50\u5427\u3002
\n1. \u672a\u4f7f\u7528 screen \u65f6\u65b0\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u6811<\/p>\n

[root@pvcent107 ~]# ping www.google.com &\r\n[1] 9499\r\n[root@pvcent107 ~]# pstree -H 9499\r\ninit\u2500\u252c\u2500Xvnc\r\n     \u251c\u2500acpid\r\n     \u251c\u2500atd\r\n     \u251c\u25002*[sendmail]\t\r\n     \u251c\u2500sshd\u2500\u252c\u2500sshd\u2500\u2500\u2500bash\u2500\u2500\u2500pstree\r\n     \u2502       \u2514\u2500sshd\u2500\u2500\u2500bash\u2500\u2500\u2500ping\r\n<\/pre>\n

\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u672a\u4f7f\u7528 screen \u65f6\u6211\u4eec\u6240\u5904\u7684 bash \u662f sshd \u7684\u5b50\u8fdb\u7a0b\uff0c\u5f53 ssh \u65ad\u5f00\u8fde\u63a5\u65f6\uff0cHUP \u4fe1\u53f7\u81ea\u7136\u4f1a\u5f71\u54cd\u5230\u5b83\u4e0b\u9762\u7684\u6240\u6709\u5b50\u8fdb\u7a0b\uff08\u5305\u62ec\u6211\u4eec\u65b0\u5efa\u7acb\u7684 ping \u8fdb\u7a0b\uff09\u3002
\n2. \u4f7f\u7528\u4e86 screen \u540e\u65b0\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u6811<\/p>\n

[root@pvcent107 ~]# screen -r Urumchi\r\n[root@pvcent107 ~]# ping www.ibm.com &\r\n[1] 9488\r\n[root@pvcent107 ~]# pstree -H 9488\r\ninit\u2500\u252c\u2500Xvnc\r\n     \u251c\u2500acpid\r\n     \u251c\u2500atd\r\n     \u251c\u2500screen\u2500\u2500\u2500bash\u2500\u2500\u2500ping\r\n     \u251c\u25002*[sendmail]\r\n<\/pre>\n

\u800c\u4f7f\u7528\u4e86 screen \u540e\u5c31\u4e0d\u540c\u4e86\uff0c\u6b64\u65f6 bash \u662f screen \u7684\u5b50\u8fdb\u7a0b\uff0c\u800c screen \u662f init\uff08PID\u4e3a1\uff09\u7684\u5b50\u8fdb\u7a0b\u3002\u90a3\u4e48\u5f53 ssh \u65ad\u5f00\u8fde\u63a5\u65f6\uff0cHUP \u4fe1\u53f7\u81ea\u7136\u4e0d\u4f1a\u5f71\u54cd\u5230 screen \u4e0b\u9762\u7684\u5b50\u8fdb\u7a0b\u4e86\u3002<\/p>\n

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

\u73b0\u5728\u51e0\u79cd\u65b9\u6cd5\u5df2\u7ecf\u4ecb\u7ecd\u5b8c\u6bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u6765\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6848\u3002nohup\/setsid \u65e0\u7591\u662f\u4e34\u65f6\u9700\u8981\u65f6\u6700\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0cdisown \u80fd\u5e2e\u52a9\u6211\u4eec\u6765\u4e8b\u540e\u8865\u6551\u5f53\u524d\u5df2\u7ecf\u5728\u8fd0\u884c\u4e86\u7684\u4f5c\u4e1a\uff0c\u800c screen \u5219\u662f\u5728\u5927\u6279\u91cf\u64cd\u4f5c\u65f6\u4e0d\u4e8c\u7684\u9009\u62e9\u4e86\u3002<\/p>\n

\u672c\u6587\u539f\u521b\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/process-run-in-background.html<\/a>\u4f5c\u8005\uff1a\u5cb3\u56fd\u5e05\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u60f3\u8ba9\u8fdb\u7a0b\u5728\u65ad\u5f00\u8fde\u63a5\u540e\u4f9d\u7136\u4fdd\u6301\u8fd0\u884c\uff1f\u5982\u679c\u8be5\u8fdb\u7a0b\u5df2\u7ecf\u5f00\u59cb\u8fd0\u884c\u4e86\u8be5\u5982\u4f55\u8865\u6551\uff1f \u5982\u679c\u6709\u5927\u91cf\u8fd9\u7c7b\u9700\u6c42\u5982\u4f55\u7b80\u5316\u64cd\u4f5c\uff1f […]<\/p>\n","protected":false},"author":63,"featured_media":13942,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-8287","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\/8287","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\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=8287"}],"version-history":[{"count":14,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/8287\/revisions"}],"predecessor-version":[{"id":249386,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/8287\/revisions\/249386"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/13942"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=8287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=8287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=8287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}