Reputation: 281
I have following function:
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)) }
The sshcommand function looks like this:
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); }
My problem is that the first update function wait till /var/script/config.sh
has finished.
But in some of my cases it takes very long, so I just want to sent the command and let it work in the background.
I tried to change it to /var/script/config.sh | at now + 1 minute
but its the same result..
Any Ideas?
Upvotes: 1
Views: 2516
Reputation: 75629
Try using &
with your command:
$sshcommand = '/var/script/config.sh &';
bash
man page says:
If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.
Ensure shell you are using for user that is being used by ssh, supports that.
Alternatively you can try with nohup
:
$sshcommand = 'nohup /var/script/config.sh';
This may also be shell dependant. bash
works. Not sure about i.e. plain sh
though.
Upvotes: 3