{"id":1764,"date":"2016-10-06T16:35:51","date_gmt":"2016-10-06T08:35:51","guid":{"rendered":"https:\/\/blog.vimge.com\/?p=1764"},"modified":"2017-06-08T17:05:13","modified_gmt":"2017-06-08T09:05:13","slug":"php-daemon-processer","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/linux\/php-daemon-processer.html","title":{"rendered":"\u521b\u5efaPHP\u5b88\u62a4\u8fdb\u7a0b\u540e\u53f0\u8fd0\u884c\uff0c\u76d1\u63a7\u72b6\u6001\u9000\u51fa\u81ea\u52a8\u91cd\u542f"},"content":{"rendered":"

\u7ecf\u5e38\u7528\u5230PHP\u811a\u672c\u5199\u7684\u5c0f\u7a0b\u5e8f\u9700\u8981\u5728\u670d\u52a1\u5668\u7aef\u4fdd\u6301\u540e\u53f0\u8fd0\u884c\uff0c\u9664\u4e86\u4f7f\u7528screen\u7b49\u5de5\u5177\u5916\uff0c\u8fd9\u91cc\u4ecb\u7ecd\u4f7f\u7528Linux shell\u7f16\u5199\u4e00\u4e2a\u5c0f\u5de5\u5177\u5b9e\u73b0PHP\u811a\u672c\u7a0b\u5e8f\u5728\u540e\u53f0\u4fdd\u6301\u8fd0\u884c\uff0c\u4e0d\u53d7\u7ec8\u7aef\u9000\u51fa\u5f71\u54cd\uff0c\u4e14\u80fd\u81ea\u52a8\u76d1\u6d4b\u811a\u672c\u8fd0\u884c\u72b6\u6001\uff0c\u5bf9\u56e0\u5176\u4ed6\u56e0\u7d20\u5bfc\u81f4\u7a0b\u5e8f\u9000\u51fa\u7684\u60c5\u51b5\u81ea\u52a8\u8fdb\u884c\u811a\u672c\u91cd\u542f\uff0c\u5b9e\u73b0\u5b88\u62a4\u8fd0\u884c.<\/p>\n

\u521b\u5efa\u4e00\u4e2ashell\u811a\u672c daemon.sh\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

#!\/bin\/bash<\/p>\n

count=`ps -ef | grep daemon.php | grep -v “grep” | wc -l` #\u641c\u7d22\u8fdb\u7a0b\u67e5\u770b\u662f\u5426\u5b58\u5728\u6b63\u5728\u6267\u884c\u7684\u8fdb\u7a0b
\necho $count
\nif [ $count -lt 1 ]; then
\nulimit -c unlimited
\nnohup \/usr\/local\/bin\/php \/root\/test\/daemon.php & #\u6ce8\u610f\u811a\u672c\u8def\u5f84\u9700\u8981\u586b\u5199\u7edd\u5bf9\u8def\u5f84<\/p>\n

echo $(date), start success >> \/root\/test\/daemon.log #\u8bb0\u5f55\u65e5\u5fd7
\nelse
\necho $(date), running >> \/root\/test\/daemon.log
\nfi<\/p><\/blockquote>\n

daemon.php \u4e3a\u6211\u4eec\u7684PHP\u5c0f\u7a0b\u5e8f\u5165\u53e3\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u4fee\u6539<\/p>\n

\u4e3a\u811a\u672c\u6dfb\u52a0\u6267\u884c\u6743\u9650<\/p>\n

$ chmod +x daemon.sh<\/p><\/blockquote>\n

\u4e3a\u5b9e\u73b0\u81ea\u52a8\u76d1\u6d4b\u529f\u80fd\uff0c\u9700\u8981\u501f\u52a9 LInux\u7684 crontab \u5b9a\u65f6\u6267\u884c\u811a\u672c\uff0c\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1\u6bcf\u5206\u949f\u6267\u884c\u4e00\u6b21<\/p>\n

* * * * * \/root\/test\/daemon.sh<\/p><\/blockquote>\n

 <\/p>\n

\u53ef\u4ee5\u6539\u8fdb\u7684\u5730\u65b9<\/p>\n

    \n
  1. \u53ef\u4ee5\u901a\u8fc7netstat -lnp \u68c0\u6d4b\u7aef\u53e3\u662f\u5426\u5728\u76d1\u542c\uff0c\u5982\u679c\u672a\u5728\u76d1\u542c\uff0c\u5219\u6267\u884crestart<\/li>\n
  2. \u901a\u8fc7\u4e00\u4e2acheck.php\u53d1\u9001\u4e00\u6bb5\u5e26\u6709\u903b\u8f91\u7684\u8bf7\u6c42\uff0c\u8bd5\u63a2\u670d\u52a1\u5668\u662f\u5426\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5982\u679c\u65e0\u6cd5\u5de5\u4f5c\uff0c\u6267\u884crestart<\/li>\n
  3. \u4f7f\u7528supervisor\u76d1\u63a7\u8fdb\u7a0b\u7684\u5de5\u5177<\/li>\n
  4. \u5982\u679c\u5728Docker\u5bb9\u5668\u4e2d\u4f7f\u7528\uff0c\u53ef\u4ee5\u5728docker run\u65f6\u589e\u52a0\u53c2\u6570–restart=always<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"

    \u7ecf\u5e38\u7528\u5230PHP\u811a\u672c\u5199\u7684\u5c0f\u7a0b\u5e8f\u9700\u8981\u5728\u670d\u52a1\u5668\u7aef\u4fdd\u6301\u540e\u53f0\u8fd0\u884c\uff0c\u9664\u4e86\u4f7f\u7528screen\u7b49\u5de5\u5177\u5916\uff0c\u8fd9\u91cc\u4ecb\u7ecd\u4f7f\u7528Linux […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,9],"tags":[157,158],"_links":{"self":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1764"}],"collection":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/comments?post=1764"}],"version-history":[{"count":7,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1764\/revisions"}],"predecessor-version":[{"id":1771,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1764\/revisions\/1771"}],"wp:attachment":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/media?parent=1764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/categories?post=1764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/tags?post=1764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}