PHP:如何发送ssh命令而不等待它被执行?
我有以下功能:PHP:如何发送ssh命令而不等待它被执行?,php,ssh,Php,Ssh,我有以下功能: public function update($id){ $data = $this->client_model->get_client_by_id($id); $sshport = $data['ssh_port']; $sshcommand = '/var/script/config.sh'; $this->sshcommand($sshport, $sshcommand); $this->session->set_
public function update($id){
$data = $this->client_model->get_client_by_id($id);
$sshport = $data['ssh_port'];
$sshcommand = '/var/script/config.sh';
$this->sshcommand($sshport, $sshcommand);
$this->session->set_flashdata('msg', 'Config has been sent');
redirect(base_url('admin/edit/'.$id)) }
sshcommand函数如下所示:
private function sshcommand($port, $command) {
$remotecommand = 'ssh -q root@localhost -o "StrictHostKeyChecking=no" -p'.$port.' "'.$command.'" 2> /dev/null';
$connection = ssh2_connect('controller.server.example.org', 22);
ssh2_auth_pubkey_file($connection, 'root','/root/.ssh/id_rsa.pub','/root/.ssh/id_rsa');
ssh2_exec($connection, $remotecommand); }
我的问题是第一个更新函数要等到/var/script/config.sh完成
但在我的一些情况下,这需要很长时间,所以我只想发送命令,让它在后台工作
我试图在现在+1分钟时将其更改为/var/script/config.sh,但结果相同
有什么想法吗?尝试使用命令(&W):
$sshcommand = '/var/script/config.sh &';
bash手册页说:
如果命令被控制操作符&终止,shell将在子shell的后台执行该命令。shell不会等待命令完成,返回状态为0
确保您为ssh正在使用的用户使用的shell支持该功能
或者,您可以尝试使用nohup:
这也可能取决于外壳。bash可以工作。不确定,即普通sh。这主要是相关的。这有用吗?谢谢,但是我也尝试重定向stdout和stderr,但没有成功。在第一刻,我认为这就是它,但是脚本没有执行:确保使用支持它的shell。或者,您可以使用nohup。我编辑了答案。谢谢,似乎问题出在config.sh中的wget…不知怎的。。。不使用&wget工作,如果我将wget切换为curl,脚本也可以工作。但遗憾的是,curl无法处理我需要的东西,所以我必须找出wget的确切问题所在。好的,我在现在+1分钟时得到了-f/var/data/setupplaylist.sh,它起了作用。你应该找到罪魁祸首,而不是用变通方法掩盖它。尽量使wget冗长,并将其输出记录在某个地方。然后检查它为什么失败
$sshcommand = 'nohup /var/script/config.sh';