Remote shell exec not finishing running. Random how far it gets. Centos 7.

January 4, 2016 1.9k views
Linux Commands PHP DigitalOcean Linux Basics CentOS

Hey Guys,

Can anyone shed some light on why Digital Oceans VPS servers don't like finishing running ssh exec commands run from php?

It works fine on hosts other than digital ocean, also using centos 7. and I can get putty to run the commands, but not if I connect through a php function.
I have been using phpseclib.

I have had to split my php functions up into little bits and run them one at a time.
Some of them don't ever finish. like when I try to install sentora.
It seems random how far it gets into the installation.

The Code works fine:

$ssh = new Net_SSH2("$domain");
if (!$ssh->login($username, $password)) {
    exit('Login Failed');
}

echo $ssh->exec("wget http://sentora.org/install");
echo $ssh->exec("chmod +x install");
echo $ssh->exec("./install -t Europe/Germany -d host.$domain -i $ip");
2 comments
  • That is odd. I haven't used this particular library but I've done some significant scripting using Ruby's ssh library to do similar things. To narrow this down I have a couple ideas

    1. Try changing echo $ssh->exec("./install -t Europe/Germany -d host.$domain -i $ip");

    to

    echo $ssh->exec("./install -t Europe/Germany -d host.$domain -i $ip > /somedirectory/output.txt");
    

    This should log the output of the command to the specified file allowing you to take a look and possibly see what happened.

    If you are simply installing a control panel on newly created droplets it might be worth looking into user-data. This allows you to pass a shell script to your newly created droplet that is run immediately on the first boot. We have a collection of pre-built user-data scripts that might be helpful as a reference.

    If going in this direction you could implement a system like the following:

    Control server (with your php script) kicks off droplet creation via the API, passing a user-data block with the domain in it. You can gather the new droplet's IP address from droplet meta-data via curl. You can include a line in your user-data script to have the new droplet use curl or another method to report back to your control script that provisioning is complete.

    Update CloudInit link and conclusion Metadata is a service provided to DigitalOcean droplets that allows a droplet to access data about itself, i.e. its metadata. Examples of available droplet metadata include user-provided user data, droplet ID, data center region,...
  • Thanks Ryan, I will look into this asap and share my results.

    Best wishes
    J.

1 Answer

This question was answered by @ryanpq:

That is odd. I haven't used this particular library but I've done some significant scripting using Ruby's ssh library to do similar things. To narrow this down I have a couple ideas

  1. Try changing echo $ssh->exec("./install -t Europe/Germany -d host.$domain -i $ip");

to

echo $ssh->exec("./install -t Europe/Germany -d host.$domain -i $ip > /somedirectory/output.txt");

This should log the output of the command to the specified file allowing you to take a look and possibly see what happened.

If you are simply installing a control panel on newly created droplets it might be worth looking into user-data. This allows you to pass a shell script to your newly created droplet that is run immediately on the first boot. We have a collection of pre-built user-data scripts that might be helpful as a reference.

If going in this direction you could implement a system like the following:

Control server (with your php script) kicks off droplet creation via the API, passing a user-data block with the domain in it. You can gather the new droplet's IP address from droplet meta-data via curl. You can include a line in your user-data script to have the new droplet use curl or another method to report back to your control script that provisioning is complete.

View the original comment

Update CloudInit link and conclusion Metadata is a service provided to DigitalOcean droplets that allows a droplet to access data about itself, i.e. its metadata. Examples of available droplet metadata include user-provided user data, droplet ID, data center region,...
Have another answer? Share your knowledge.