{"id":82,"date":"2019-11-08T12:22:35","date_gmt":"2019-11-08T04:22:35","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=82"},"modified":"2019-11-08T12:22:35","modified_gmt":"2019-11-08T04:22:35","slug":"yii2%e7%9a%84console%e5%ae%9a%e6%97%b6%e4%bb%bb%e5%8a%a1%e5%88%9b%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2019\/11\/08\/yii2%e7%9a%84console%e5%ae%9a%e6%97%b6%e4%bb%bb%e5%8a%a1%e5%88%9b%e5%bb%ba\/","title":{"rendered":"yii2\u7684Console\u5b9a\u65f6\u4efb\u52a1\u521b\u5efa"},"content":{"rendered":"\n<p>Yii2\u7684\u5b9a\u65f6\u4efb\u52a1\u53ef\u4ee5\u6709\u4e24\u79cd\u5199\u6cd5\uff0c\u539f\u7406\u90fd\u662f\u901a\u8fc7\u670d\u52a1\u5668\u7684\u5b9a\u65f6\u4efb\u52a1\u53bb\u8c03\u7528<\/p>\n\n\n\n<p>1.\u901a\u8fc7\u8c03\u7528\u6307\u5b9a\u7684URL\u8bbf\u95ee \u5c31\u76f8\u5f53\u4e8e\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<\/p>\n\n\n\n<p>2.\u901a\u8fc7console\u8c03\u7528<\/p>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u5c31\u6765\u8bf4\u8bf4Console \u662f\u5982\u4f55\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u7684<\/p>\n\n\n\n<p>\u4e00\u3001\u9996\u5148\u5728\u521b\u5efaControlle \u5e76\u7ee7\u627f yii\\console\\Controller;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace console\\controllers;\n\nuse yii\\console\\Controller;\n\nclass TestController extends Controller {\n\npublic function actionIndex()\n\n{\n\necho \"hello world\";\n\n}\n\n}<\/code><\/pre>\n\n\n\n<p>\u5728yii\u6839\u76ee\u5f55\u4e0b\uff0c\u547d\u4ee4\u884c\u6267\u884c<\/p>\n\n\n\n<p>yii test\/index<\/p>\n\n\n\n<p>\u5982\u679c\u662fclass AaaBbbController function ActionAaaBbb<\/p>\n\n\n\n<p>yii aaa-bbb\/aaa-bbb<\/p>\n\n\n\n<p>linux\u4e0b\uff0c\u8fd0\u884ccrontab -e<\/p>\n\n\n\n<p>30 21 * * * \/usr\/local\/php\/bin\/php \/your_project_path\/yii test\/test<\/p>\n\n\n\n<p>\u4e8c\u3001\u7528\u547d\u4ee4\u884ccmd\u6d4b\u8bd5\u662f\u5426\u6210\u529f<\/p>\n\n\n\n<p>\uff08\u4e00\u5b9a\u8981\u770b\u597d\u8def\u5f84\u53bb\u6267\u884c   yii  \u8fd9\u4e2a\u6587\u4ef6              \u4e00\u5b9a\u8981\u5199\u7edd\u5bf9\u8def\u5f84\uff09<\/p>\n\n\n\n<p>\/\u4f60\u7684\u6587\u4ef6\u5939\u8def\u5f84\/yii      \u4f60\u7684\u6587\u4ef6\u5939\u8def\u5f84\/console\/test(\u63a7\u5236\u5668)\/index(\u65b9\u6cd5)<\/p>\n\n\n\n<p>\u6216\u8005\u8fdb\u5165\u7684\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u4e0b,\u4f8b\u5982 D:\\wamp\\www\\smartSPACE&gt;yii crontab\/copy-db,\u4f60\u9700\u8981\u914d\u7f6e\u4f60\u7684php\u73af\u5883\u53d8\u91cf<\/p>\n\n\n\n<p>\u4e0b\u4f8b\u4e3a  \u5728console\u6587\u4ef6\u5939\u4e0b\u6267\u884c\u63a7\u5236\u5668\u548c\u65b9\u6cd5    <\/p>\n\n\n\n<p>\u4e09\u3001\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1<\/p>\n\n\n\n<p>linux\u4e0b\uff0c\u8fd0\u884ccrontab -e<\/p>\n\n\n\n<p>30 21 * * * \/usr\/local\/php\/bin\/php \/your_project_path\/yii test\/test<\/p>\n\n\n\n<p>*\/1 * * * * \/usr\/local\/php\/bin\/php \/home\/wwwroot\/smartSPACE\/yii crontab\/copydb<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6839\u636e\u81ea\u5df1\u9700\u6c42\u586b\u5199<\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u8868\u793a\u6bcf\u665a\u768421:30\u6267\u884c\u4e0a\u9762\u7684\u5b9a\u65f6\u7a0b\u5e8f<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u5b9a\u65f6\u4efb\u52a1\u8bbe\u7f6e\u7684\u4e00\u4e9b\u57fa\u672c\u4ecb\u7ecd<\/p>\n\n\n\n<p>\u57fa\u672c\u683c\u5f0f :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>* * * * command<\/li><\/ul>\n\n\n\n<p>\u5206 \u65f6 \u65e5 \u6708 \u5468 \u547d\u4ee4<\/p>\n\n\n\n<p>\u7b2c1\u5217\u8868\u793a\u5206\u949f1\uff5e59 \u6bcf\u5206\u949f\u7528*\u6216\u8005 *\/1\u8868\u793a<\/p>\n\n\n\n<p>\u7b2c2\u5217\u8868\u793a\u5c0f\u65f61\uff5e23\uff080\u8868\u793a0\u70b9\uff09<\/p>\n\n\n\n<p>\u7b2c3\u5217\u8868\u793a\u65e5\u671f1\uff5e31<\/p>\n\n\n\n<p>\u7b2c4\u5217\u8868\u793a\u6708\u4efd1\uff5e12<\/p>\n\n\n\n<p>\u7b2c5\u5217\u6807\u8bc6\u53f7\u661f\u671f0\uff5e6\uff080\u8868\u793a\u661f\u671f\u5929\uff09<\/p>\n\n\n\n<p>\u7b2c6\u5217\u8981\u8fd0\u884c\u7684\u547d\u4ee4<\/p>\n\n\n\n<p>crontab\u6587\u4ef6\u7684\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p>30 21 * * * \/usr\/local\/etc\/rc.d\/lighttpd restart #\u4e0a\u9762\u7684\u4f8b\u5b50\u8868\u793a\u6bcf\u665a\u768421:30\u91cd\u542fapache\u3002<\/p>\n\n\n\n<p>45 4 1,10,22 * * \/usr\/local\/etc\/rc.d\/lighttpd restart #\u4e0a\u9762\u7684\u4f8b\u5b50\u8868\u793a\u6bcf\u67081\u300110\u300122\u65e5\u76844 : 45\u91cd\u542fapache\u3002<\/p>\n\n\n\n<p>10 1 * * 6,0 \/usr\/local\/etc\/rc.d\/lighttpd restart #\u4e0a\u9762\u7684\u4f8b\u5b50\u8868\u793a\u6bcf\u5468\u516d\u3001\u5468\u65e5\u76841 : 10\u91cd\u542fapache\u3002<\/p>\n\n\n\n<p>0,30 18-23 * * * \/usr\/local\/etc\/rc.d\/lighttpd restart #\u4e0a\u9762\u7684\u4f8b\u5b50\u8868\u793a\u5728\u6bcf\u592918 : 00\u81f323 : 00\u4e4b\u95f4\u6bcf\u969430\u5206\u949f\u91cd\u542fapache\u3002<\/p>\n\n\n\n<p>0 23 * * 6 \/usr\/local\/etc\/rc.d\/lighttpd restart #\u4e0a\u9762\u7684\u4f8b\u5b50\u8868\u793a\u6bcf\u661f\u671f\u516d\u768411 : 00 pm\u91cd\u542fapache\u3002<\/p>\n\n\n\n<p>0 *\/1 * * * \/usr\/local\/etc\/rc.d\/lighttpd restart #\u6bcf\u4e00\u5c0f\u65f6\u91cd\u542fapache<\/p>\n\n\n\n<p>\u4f8b\u5b50:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*\/1 * * * * \/usr\/local\/php\/bin\/php \/home\/wwwroot\/smartSPACE\/yii crontab\/copy-db>>\/home\/wwwroot\/smartSPACE\/commands\/test.txt\n\n\u6ce8\u91ca:\n\u4e00\u5206\u949f\u91cd\u590d    *\/1 * * * *\nphp\u76ee\u5f55      \/usr\/local\/php\/bin\/php\n\u9879\u76ee\u76ee\u5f55      \/home\/wwwroot\/smartSPACE\/\n\u8fd0\u884c\u7684\u65b9\u6cd5   yii crontab\/copy-db\n\u7ed3\u679c\u503c\u5199\u5165\u90a3\u4e2a\u6587\u4ef6  \/home\/wwwroot\/smartSPACE\/commands\/test.txt<\/code><\/pre>\n\n\n\n<p>linux \u4e0a\u8ddfwindows\u7c7b\u4f3c<\/p>\n\n\n\n<p>\/usr\/local\/php\/bin\/php \/home\/wwwroot\/smartSPACE\/yii crontab\/copy-db<\/p>\n\n\n\n<p>\/usr\/local\/php\/bin\/php\u4f60\u7684linux\u4e0b\u7684php\u73af\u5883\u4f4d\u7f6e \/home\/wwwroot\/smartSPACE\/yii crontab\/copy-db\u4f60\u7684\u9879\u76ee\u76ee\u5f55<\/p>\n\n\n\n<p>\u7ed9\u4e2aphp\u4ee3\u7801\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php \n\n\/**\n * @ \u5b9a\u65f6\u4efb\u52a1\n * @\n * @\n *\/\n\nnamespace app\\commands;\n\nuse yii\\console\\Controller;\n\/\/ use MongoDB\\BSON\\ObjectID;\n\nclass CrontabController  extends Controller\n{\n    \/**\n     * \u5b9a\u65f6\u6e05\u7406mongodb\u6570\u636e\n     *\/\n    public function actionClearMongo()\n    {\n        $query = new \\yii\\mongodb\\Query();\n        $time_end = strtotime(date('2018-06-20'));\n        $time_begin = strtotime(\"2018-05-30\");\n        $collection = \\Yii::$app->mongodb->getCollection('publish_message');\n        $i = 1;\n        while ($time_begin &lt; $time_end){\n            $date = date(\"Y-m-d\", $time_begin);\n            $start_time = strtotime($date.\" 00:00:00\");\n            $end_time = strtotime($date.\" 23:30:00\");\n            \/\/\u6e05\u9664\u6bcf\u592923:30\u524d\u7684\u6570\u636e\n            $where = [\n                \"between\",\"payload.timestamp\", $start_time, $end_time\n            ];\n            $time_begin +=24*3600;\n\/\/         var_dump(new \\MongoDB\\BSON\\UTCDateTime($end_time.'000'));die;\n            $data = $query->from('publish_message')->where($where)->orderBy('created asc')->all();\n\/\/             $primaryVal = new \\yii\\mongodb\\MongoId('000000000000000000000001');\n\/\/             dump($primaryVal);die;\n\/\/             $collection = \\Yii::$app->mongodb->getCollection('publish_message');\n\n\/\/             var_dump((string) new \\yii\\MongoDB\\BSON\\ObjectId('000000000000000000000001'));\n\/\/             $data = $collection->find([]);\n\n            foreach ($data as $v){\n\/\/                 var_dump($v);\n                $oid = (string)$v['_id'];\n                echo $i.'-'.$oid.'\n    ';\n                $collection->remove(['_id' => $v['_id']]);\n                $i++;\n            }\n        }\n        die;\n    }\n\n    \/**\n     * \u5b9a\u65f6\u4efb\u52a1\u83b7\u53d6mondb\u5185\u6545\u969c\u6d88\u606f\n     * create dahai\n     * time 2018\/07\/18\n     *\/\n    public function actionCopyDb(){\n        $query = new \\yii\\mongodb\\Query();\n\n        \/\/\u5148\u83b7\u53d6\u4e0a\u4e00\u6b21\u7684\u7ed3\u675f\u65f6\u95f4\n        $last_result = $query->select(['end_time'])->from('timed_task')->orderBy('_id desc')->limit(1)->one();\n        $last_time = $last_result['end_time'];\n        $start_time = $last_time;\n        $end_time = time();\n\n        $collection = \\Yii::$app->mongodb->getCollection('timed_task');\n        $collection->insert(['end_time' => $end_time]);\n\n\n        $query->andWhere([\n            \"between\",\"payload.timestamp\", $start_time, $end_time\n        ]);\n        $query->andWhere(['in', 'payload.desired.arrays.0.status.event', [1,2,3,4,5,6,7]]);\n        $result = $query->select(['created', 'payload'])->from('publish_message')->orderBy('created asc')->all();\n        if ($result){\n            \/\/\u6545\u969c\u7c7b\u578b,\u8bbe\u5907\u522b\u79f0,\u5f00\u5173\u522b\u79f0,\u6545\u969c\u65f6\u95f4\n            foreach ($result as $k => $l)\n            {\n                \/\/\u5bf9\u5e94\u76f8\u5173\u7528\u6237\u901a\u77e5\u6545\u969c\u6d88\u606f\n                $deviceid = $l['payload']['deviceid'];\n                $Equipment= \\app\\models\\Equipment::find()->select('id')->where(['serial_number'=>$deviceid])->one();\n                $EquipmentUser =\\app\\models\\EquipmentUser::find()->select('user_id')->where(['equipment_id'=>$Equipment->id])->asArray()->all();\n                $user_ids = array_column($EquipmentUser,'user_id');\n                $created = $l['payload']['timestamp'] > 0 ? date('Y-m-d H:i', $l['payload']['timestamp']) : '---';\n                $event_desc = \\app\\services\\VStatusLog::$event[$l['payload']['desired']['arrays'][0]['status']['event']];\n                \/\/\u67e5\u8be2\u522b\u540d\n                $eAlias = \\app\\models\\Alias::findOne(['type'=>1,'extend_id'=>$l['payload']['deviceid'],'user_id'=>$this->uid]); \/\/\u8bbe\u5907\u522b\u540d\n                $sAlias = \\app\\models\\Alias::findOne(['type'=>2,'extend_id'=>$l['payload']['desired']['arrays'][0]['device']['id'],'user_id'=>$this->uid]);\/\/\u5f00\u5173\u522b\u540d\n                $alias1 = $eAlias?$eAlias['alias']:$l['payload']['deviceid'];\n                $alias2 = $sAlias?$eAlias['alias']:'\u5f00\u5173'.$l['payload']['desired']['arrays'][0]['device']['index'];\n                while ($user_id = array_shift($user_ids)){\n                    $FaultMessage = new \\app\\models\\FaultMessage();\n                    $FaultMessage->user_id = $user_id;\n                    $FaultMessage->message_id = ($l['_id'])->__toString();\n                    $FaultMessage->created = $created;\n                    $FaultMessage->event = $l['payload']['desired']['arrays'][0]['status']['event'];\n                    $FaultMessage->event_desc = $event_desc;\n                    $FaultMessage->equipment_alias = $alias1;\n                    $FaultMessage->switch_alias = $alias2;\n                    $FaultMessage->create_time = time();\n                    $FaultMessage->save();\n                }\n            }\n         }else{\n            echo \"\u6ca1\u6709\".date(\"Y-m-d H:i:s\").\"\\n\";\n            return \"\u6ca1\u6709\u4e1c\u897f\";\n        }\n        echo \"\u6709\".date(\"Y-m-d H:i:s\").\"\\n\";\n        return \"\u6709\u4e1c\u897f\";\n    }\n\n}<\/code><\/pre>\n\n\n\n<p>yii2\u4e2d,\u5173\u4e8e\u4f60\u52a0\u8f7d\u4e86\u4e00\u4e9b\u6570\u636e\u5e93\u6216\u8005\u6269\u5c55\u4f9d\u8d56,\u4f60\u9700\u8981\u5728\u6dfb\u52a0config-console.php\u6dfb\u52a0\u4f60\u5728web.php\u76f8\u540c\u7684\u914d\u7f6e\u5373\u53ef<\/p>\n\n\n\n<p>\u4f8b\u5982\u6211\u8fd9\u4e2a\u62a5\u9519:yii\\di\\ServiceLocator->get(&#8216;mongodb&#8217;) yii\\mongodb\\Query->all()\u5c31\u662f\u56e0\u4e3a\u8fd9\u4e2a\u5bfc\u81f4<br> \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<br> \u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/qq_27229113\/article\/details\/81103227<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yii2\u7684\u5b9a\u65f6\u4efb\u52a1\u53ef\u4ee5\u6709\u4e24\u79cd\u5199\u6cd5\uff0c\u539f\u7406\u90fd\u662f\u901a\u8fc7\u670d\u52a1\u5668\u7684\u5b9a\u65f6\u4efb\u52a1\u53bb\u8c03\u7528 1.\u901a\u8fc7\u8c03\u7528\u6307\u5b9a\u7684URL\u8bbf\u95ee \u5c31\u76f8\u5f53\u4e8e\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee 2.\u901a\u8fc7console\u8c03\u7528 \u4e0b\u9762\u6211\u4eec\u5c31\u6765\u8bf4\u8bf4Console \u662f\u5982\u4f55\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u7684 \u4e00\u3001\u9996\u5148\u5728\u521b\u5efaControlle \u5e76\u7ee7\u627f yii\\console\\Controller; \u5728yii\u6839\u76ee\u5f55\u4e0b\uff0c\u547d\u4ee4\u884c\u6267\u884c yii test\/index \u5982\u679c\u662fclass AaaBbbController function ActionAaaBbb yii aaa-bbb\/aaa-bbb linux\u4e0b\uff0c\u8fd0\u884ccrontab -e 30 21 * * * \/usr\/local\/php\/bin\/php \/your_project_path\/yii test\/test \u4e8c\u3001\u7528\u547d\u4ee4\u884ccmd\u6d4b\u8bd5\u662f\u5426\u6210\u529f \uff08\u4e00\u5b9a\u8981\u770b\u597d\u8def\u5f84\u53bb\u6267\u884c yii \u8fd9\u4e2a\u6587\u4ef6 \u4e00\u5b9a\u8981\u5199\u7edd\u5bf9\u8def\u5f84\uff09 \/\u4f60\u7684\u6587\u4ef6\u5939\u8def\u5f84\/yii \u4f60\u7684\u6587\u4ef6\u5939\u8def\u5f84\/console\/test(\u63a7\u5236\u5668)\/index(\u65b9\u6cd5) \u6216\u8005\u8fdb\u5165\u7684\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u4e0b,\u4f8b\u5982 D:\\wamp\\www\\smartSPACE&gt;yii crontab\/copy-db,\u4f60\u9700\u8981\u914d\u7f6e\u4f60\u7684php\u73af\u5883\u53d8\u91cf \u4e0b\u4f8b\u4e3a \u5728console\u6587\u4ef6\u5939\u4e0b\u6267\u884c\u63a7\u5236\u5668\u548c\u65b9\u6cd5 \u4e09\u3001\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1 linux\u4e0b\uff0c\u8fd0\u884ccrontab [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-yii2"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}