phplinuxbashcommand-linessh2-exec

Run some Linux command in PHP trail together


I wrote some php code that connected to vps through ssh

I know ssh2_exec can do it, but if I want run many command like:

ssh2_exec($connection, 'cd /home/ubuntu/');
ssh2_exec($connection, 'mkdir folder');
ssh2_exec($connection, 'cd folder');
ssh2_exec($connection, 'touch test.txt');
.
.
.

It doesn't work and only do first command. how can I run some many command trail together?


Solution

  • you can write multiple command on one line separated by ; or && So you can follow below code

    ssh2_exec($connection, 'cd /home/ubuntu/; mkdir folder; cd folder; touch test.txt');
    

    OR

    ssh2_exec($connection, 'cd /home/ubuntu/ && mkdir folder && cd folder &&touch test.txt');