1

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?

2
  • This is mostly related. Does this help? stackoverflow.com/questions/1019867/… Commented Feb 18, 2019 at 13:58
  • 1
    Thanks, but I tried to redirect stdout and stderr too with no success. Commented Feb 18, 2019 at 14:00

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

In the first moment I though this is it, but than the script is not executed :(
Ensure you use shell that supports that. Alternatively you can use nohup. I edited the answer.
thanks, it seems the problem is wget inside the config.sh..somehow... without & wget works and if I switch wget to curl the script works too. But sadly curl can't handle what I need, so I have to find out what exactly the problem with wget is.
Ok I ended up with at now + 1 minute -f /var/data/setupplaylist.sh which worked.
you should find the culprit, not mask it with workaround. Try to make your wget verbose and log its output somewhere. Then check why it fails

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.