0

I am developing an application which would require me to execute a bash script on a remote server when my users give me a command through a web interface.

I need suggestions on how I can run a bash script on a remote server when I get a command on my main application server.

P.S. The bash script will need to do some tasks which only a root user can do, like, restart NGINX.

Kanishk Dudeja
  • 173
  • 1
  • 10
  • 1
    via SSH, obviously, unless you want to develop something likes a nodejs service, which will then accept http queries & execute them :) – Anubioz Oct 06 '16 at 16:26
  • Okay so you mean to say the main server runs a bash script which uses SSH to connect to the remote server and do some tasks, right? 2 concerns in this: a) What if the SSH connection breaks? b) How do i tell main server to run a bash script? Through PHP using exec() call? – Kanishk Dudeja Oct 06 '16 at 16:36
  • 1
    You see, you mention PHP just now, how would you expect to answer your question if you don't give a single hint about your environment. To run a script without having to take care about ssh connection state use: `ssh example.com -C "nohup your_command > /dev/null &"` – Anubioz Oct 06 '16 at 16:43
  • So my main server runs NGINX with PHP-FPM and the the remote server will run NGINX too. I think I can have the source of the bash script on the remote server. And then I can do this on the main server: ssh example.com -C "nohup bash script.sh argument1 argument2 > /dev/null & – Kanishk Dudeja Oct 06 '16 at 16:46
  • 1
    Yes, just like that, but you should do `ssh-copy-id root@your_secondary_server.com` from the user, running `nginx` on the main server first in order to have ability to run remote commands – Anubioz Oct 06 '16 at 16:48
  • Yes I got it. Thank you. So how do i run the SSH command from the main server? I mean should i use exec() within PHP to run a this command? Or I should make a web request to the same system on another port, on which maybe a bash server is listening? – Kanishk Dudeja Oct 06 '16 at 16:50
  • There is no such thing a "bash server", it works via ssh. So yes, just execute it with php exec(); make sure you understand how shell security works first though to do a proper argument filtration. I recommend [escapeshellcmd](http://php.net/manual/en/function.escapeshellcmd.php) instead of exec() ;) – Anubioz Oct 06 '16 at 16:52
  • What I meant by a bash server is NGINX running on a different port and reverse proxying a bash script through FastCGI. Anyway, wouldn't a messaging system like RabbitMQ be better for a such a scenario? Main server puts a job in RabbitMQ. PHP consumer on remote server polls the job and executes it via exec() command. – Kanishk Dudeja Oct 06 '16 at 16:55
  • 2
    There are lots of better ways to do that than using bash via ssh (starting with notable [php internal ssh2_exec command](http://php.net/manual/en/function.ssh2-exec.php)), but that's offtopic since falls into service recommendation category. Just [google it](https://www.google.com/search?num=100&newwindow=1&hl=en&q=Execute+command+remote+server+from+php). – Anubioz Oct 06 '16 at 16:58

0 Answers0