## Process->exec
執行一個外部程序,此函數是exec系統調用的封裝。
~~~
bool Process->exec(string $execfile, array $args)
~~~
* `$execfile`指定可執行文件的絕對路徑,如`"/usr/bin/python"`
* `$args`是一個數組,是exec的參數列表,如`array('test.py', 123)`,相當與`python test.py 123`
執行成功后,當前進程的代碼段將會被新程序替換。子進程蛻變成另外一套程序。父進程與當前進程仍然是父子進程關系。
父進程與新進程之間可以通過可以通過標準輸入輸出進行通信,必須啟用標準輸入輸出重定向。
> `$execfile`必須使用絕對路徑,否則會報文件不存在錯誤
> 由于`exec`系統調用會使用指定的程序覆蓋當前程序,子進程需要讀寫標準輸出與父進程進行通信
> 如果未指定`redirect_stdin_stdout = true`,執行`exec`后子進程與父進程無法通信
## 調用示例
~~~
$process = new \Swoole\Process(function (\Swoole\Process $childProcess) {
// 不支持這種寫法
// $childProcess->exec('/usr/local/bin/php /var/www/project/yii-best-practice/cli/yii
t/index -m=123 abc xyz');
// 封裝 exec 系統調用
// 絕對路徑
// 參數必須分開放到數組中
$childProcess->exec('/usr/local/bin/php', ['/var/www/project/yii-best-practice/cli/yii',
't/index', '-m=123', 'abc', 'xyz']); // exec 系統調用
});
$process->start(); // 啟動子進程
~~~
父進程與`exec`子進程使用管道進行通信:
~~~
// exec - 與exec進程進行管道通信
use Swoole\Process;
$process = new Process(function (Process $worker) {
$worker->exec('/bin/echo', ['hello']);
$worker->write('hello');
}, true); // 需要啟用標準輸入輸出重定向
$process->start();
echo "from exec: ". $process->read(). "\n";
~~~
## 執行 shell 命令
`exec`方法與`PHP`提供的`shell_exec`不同,它是更底層的系統調用封裝。如果需要執行一條`shell`命令,請使用以下方法:
~~~
$worker->exec('/bin/sh', array('-c', "cp -rf /data/test/* /tmp/test/"));
~~~
- 序章
- 1.環境搭建
- PHP7源碼編譯安裝
- Swoole源碼編譯安裝
- Mysql5.7源碼安裝
- Redis安裝
- 2.搭建Echo服務器
- 3.Server服務器
- 函數列表
- Server::__construct
- Server->set
- Server->on
- Server->start
- Server->send
- WebSocket
- Server->push
- Server->exist
- Server::pack
- Server::unpack
- Server->disconnect
- Server->isEstablished
- 配置選項
- reactor_num
- worker_num
- max_request
- max_conn
- daemonize
- backlog
- log_file
- log_level
- upload_tmp_dir
- http_parse_post
- document_root
- http_compression
- 事件回調函數
- onStart
- onWorkerStart
- onConnect
- onReceive
- onPacket
- onRequest
- 請求Request
- Http\Request->$header
- Http\Request->$server
- Http\Request->$get
- Http\Request->$post
- Http\Request->$cookie
- Http\Request->$files
- Http\Request->rawContent
- Http\Request->getData
- 響應Response
- Http\Response->header
- Http\Response->cookie
- Http\Response->status
- Http\Response->redirect
- Http\Response->write
- Http\Response->sendfile
- Http\Response->end
- Http\Response->detach
- Http\Response::create
- onClose
- onOpen
- onMessage
- 創建服務器
- TCP服務器
- UDP服務器
- HTTP服務器
- WebSocket服務器
- 4.定時器Timer
- 5.進程Process
- Process::__construct
- Process->start
- Process->name
- Process->exec
- Process->write
- Process->read
- Process->setTimeout
- Process->setBlocking
- Process->useQueue
- Process->statQueue
- Process->freeQueue
- Process->push
- Process->pop
- Process->close
- Process->exit
- Process::kill
- Process::wait
- Process::daemon
- Process::signal
- 6.內存Memory
- Table
- Table->__construct
- Table->column
- Table->create
- Table->set
- Table->incr
- Table->decr
- Table->get
- Table->exist
- Table->count
- Table->del
- Channel
- Channel->__construct
- Channel->push
- Channel->pop
- Channel->stats
- 7.協程Coroutine
- Coroutine
- Coroutine::list
- Coroutine::set
- Coroutine::stats
- Coroutine::create
- Coroutine::exist
- Coroutine::getCid
- Coroutine::getContext
- Coroutine::defer
- Coroutine::getBackTrace
- Coroutine::yield
- Coroutine::resume
- Coroutine::fread
- Coroutine::fgets
- Coroutine::fwrite
- Coroutine::sleep
- Coroutine::gethostbyname
- Coroutine::getaddrinfo
- Coroutine::exec
- Coroutine::readFile
- Coroutine::writeFile
- Coroutine::statvfs
- Coroutine::getPcid
- Coroutine\Channel
- Coroutine\Channel->__construct
- Coroutine\Channel->push
- Coroutine\Channel->pop
- Coroutine\Channel->stats
- Coroutine\Channel->close
- Coroutine\Channel->length
- Coroutine\Channel->isEmpty
- Coroutine\Channel->isFull
- Coroutine\Channel->$capacity
- Coroutine\Channel->$errCode
- Coroutine\Client
- Coroutine\Client->connect
- Coroutine\Client->send
- Coroutine\Client->recv
- Coroutine\Client->close
- Coroutine\Client->peek
- Coroutine\Client->set
- Coroutine\Http\Client
- Coroutine\Http\Client->get
- Coroutine\Http\Client->post
- 其他
- 并行和并發的區別
- 堆、棧、隊列