Linux – execute bash on local machine to do work on remote

bashlinuxremote-accessssh

I am thinking about writing an automated script that connects to a server (ssh), copies files from it. execute a script on the server and disconnect.

  1. Is it possible to have a script execute code (like code to execute the remote script) from the local machine? i never tried it nor know how to do it.

  2. I understand how to use scp for the most part so copying is not a problem

  3. After doing 1 i would like to know how to wait for the remote script to finish and maybe once in a while execute the script but disconnect and leave the script running on the server. How do i do that? (should this be in a separate question)

Best Answer

A1) To execute a command on a remote server:

ssh server "/usr/local/sbin/command"

Using this method, the ssh session will wait for foreground processes to exit.

A3) To execute a background process on the remote server run:

ssh server "nohup /usr/local/sbin/command &"