0

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)

4 Answers 4

4

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 &"

4
  • i dont understand the ssh server. How does it know what the connection info is and which ssh server i am referring too?
    – user274
    Jul 13, 2010 at 21:26
  • You specify it. My command is an example. Replace "server" with a hostname or IP.
    – Warner
    Jul 13, 2010 at 21:40
  • Hey is there a way to do two bash commands in a single line? i doubt ; would work and i have auth keys setup so i dont need to login each time but i am curious.
    – user274
    Jul 18, 2010 at 2:20
  • 1
    ssh server "command1 ; command2"
    – Warner
    Jul 18, 2010 at 17:05
0
  1. not in a "normal" setup, but possible if you run a grid or a cluster engine. Just copy over a script and execute it

  2. to leave the script running just daemonize (run in background with stdin and out redireced to /dev/null) i.e.

    ssh remotehost "command > /dev/null 2>&1 < /dev/null &"

if you want to wait but do other stuff then use wait like with local processes

ssh firsthost command &
ssh secondhost othercommand &
...

wait
0

In your case might be useful to change your algoritm:

...connects to a server (ssh), copies files from it. execute a script on the server and disconnect.

as follows: connects to a server (ssh), copies files from it, disconnect, background script execution starts.

To have goal here, should be nice to create separated user for copy purposes:

#useradd -p xxxx -d /home/username username

next, make some changes in this user's .bash_logout file:

if [ "$SHLVL" = 1 ]; then
  nohup /usr/local/bin/command &
fi

Proceeding this, generate ssh keys for newbie and configure remote login options the way you prefer.

Now each time copying process e.g. scp user@host:/directory/SourceFile TargetFile have been done, background task /usr/local/bin/command should be started at remote box.

1
  • "$SHLVL" - subshell counter; Jul 13, 2010 at 22:40
0

What you describe sounds rather simple (and as others have said is straightforward to implement). Off the top of my head, something like:

# with a file named after each node:
SERVERS=`ls /etc/cluster/members`
THIS_NODE=`uname -n`

REMOTE_NAME="/var/cluster/incoming/${THIS_NODE}_$$"

for (i in ${SERVERS}); do
    scp $1 ${i}:${REMOTE_NAME}
    ssh ${i} "bash ${REMOTE_NAME}"
done

However, when you get into the details of what might be ivolved and working with more than just a couple of servers, the problem starts to get very complicated, very quickly.

You might want to have a look at nrpe with Nagios for stuff which gets run regularly, or Canonical's Landscape for running ad-hoc stuff on lots of servers (one of the main functions of this is for deploying patches - but it can be used for all sorts of things - and across all sorts of OS).

HTH

C.

You must log in to answer this question.