服务器php 启动命令_服务端的cli方式运行
既然是結(jié)合tp5,咱當然要借鑒tp5的優(yōu)勢
# tp5自定義命令行
這個需要自己看tp5的官方文檔,直接搜索就能找到,自己添加command.php文件,我的如下
~~~
/**
* Time: 13:55
*/
return [
'iss\crontab\command\IssServer',
'iss\crontab\command\IssClient',
];
~~~
但是要特別注意,文件的位置,放在目錄application/command.php處,或者在入口文件中指定了配置文件目錄,則將command.php放在配置目錄下的根目錄(必須的),因為查看源碼如下:

然后再添加個實例類來調(diào)用extend擴展中的服務端類,這樣寫只是為了規(guī)范些
~~~
namespace iss\crontab\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class IssServer extends Command {
protected function configure(){
$this->setName('IssServer')->setDescription('定時任務服務端');
}
/**
* 啟動服務端服務
* @return \lib\crontab\IssServer
*/
protected function execute(Input $input,Output $output){
$issServer= new \lib\crontab\IssServer();
if($issServer){
$output->writeln('服務端啟動成功!');
}else{
$output->writeln('Sorry,服務端啟動失敗!');
}
}
}
?>
~~~
然后linux中cd到項目根目錄,因為我們要用到項目中cli跟入口文件think,所以必須先cd到項目根目錄
運行命令
`php think IssServer`
就可以將服務端實例化了,就不必在服務端類里自己new對象了
有些同學可能在安裝的時候沒有將php變量加入到環(huán)境變量中,系統(tǒng)不能識別php命令
可以運行如下命令(/usr/local/php是服務器上php的安裝目錄)同樣可以啟動
`/usr/local/php/bin/php -c /usr/local/php/etc/php.ini think IssServer`
這樣就可以在擴展中輕松使用tp5的數(shù)據(jù)庫連接等特性
# 設(shè)置程序進入后臺作為守護進程一直運行

首先是配置server的daemonize屬性并加載到服務器啟動中,開啟守護進程,接著在linux窗口運行啟動命令時在命令后加“&”即可,例如
`php think IssServer &`
~~~
class IssServer{
private $serv;
private $debug = true;
public function __construct()
{
$config = config('crontab.server');
//extract($config);
$this->serv = new \swoole_server($config['host'], $config['port']);
$this->serv->set(array(
'daemonize' => $config['daemonize'], //設(shè)置程序進入后臺作為守護進程運行
'dispatch_mode' => $config['dispatch_mode'], //指定數(shù)據(jù)包分發(fā)策略。1 => 輪循模式,收到會輪循分配給每一個worker進程 2 => 固定模式,根據(jù)連接的文件描述符分配worker。這樣可以保證同一個連接發(fā)來的數(shù)據(jù)只會被同一個worker處理 3 => 搶占模式,主進程會根據(jù)Worker的忙閑狀態(tài)選擇投遞,只會投遞給處于閑置狀態(tài)的Worker
'task_worker_num' => $config['task_worker_num'], //服務器開啟的task進程數(shù)。
'task_ipc_mode' => $config['task_ipc_mode'], //設(shè)置task進程與worker進程之間通信的方式。
'log_file' => $config['log_file']
));
$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
~~~
總結(jié)
以上是生活随笔為你收集整理的服务器php 启动命令_服务端的cli方式运行的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java删除一级域名下cookie_ja
- 下一篇: 霍尔探头对高斯计测量的影响