0

我有以下功能:

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完成。

但在我的某些情况下,这需要很长时间,所以我只想发送命令并让它在后台运行。

我试图将其更改为/var/script/config.sh | at now + 1 minute但结果相同..

有任何想法吗?

4

1 回答 1

1

尝试使用&您的命令:

$sshcommand = '/var/script/config.sh &';

bash手册页说:

如果命令由控制运算符 & 终止,则 shell 在子 shell 的后台执行命令。shell 不等待命令完成,返回状态为 0。

确保您为ssh使用的用户使用的 shell 支持这一点。

或者,您可以尝试nohup

$sshcommand = 'nohup /var/script/config.sh';

这也可能取决于外壳。bash作品。sh虽然不确定即简单。

于 2019-02-18T14:05:20.247 回答