Running script on remote server

Hi All,

I need to run a ksh script on around 200 servers(consisting of AIX,LInux,HP-UX,Solaris). The script is there in the /tmp directory of all the servers. I want want to execute the script in background on the respective servers and then exit from there. I have written something like below:

 
for i in `cat Server_List`
do ssh -q $i "nohup ksh /tmp/script.ksh -E &"
done

But i want the for loop to execute the remote script and then exit and proceed to the next server in Server_List.

For this i wrote:

 
for i in `cat Server_List`;
do ssh -q $i < "ksh /tmp/script.ksh -E &; exit";
done

But i am getting errors like below:
bash: ksh /tmp/script.ksh -E &; exit: No such file or directory
bash: ksh /tmp/script.ksh -E &; exit: No such file or directory
.... .... ..... ..... ...... ......

Can someone help me in creating a script that serves my objective.

Hi, in this moment I can't test this code, but try this way:

for i in `cat Server_List`
do 
	ssh -l login_name -q $i "nohup /tmp/script.ksh -E" &
done

That is a useless use of cat and dangerous use of backticks.

while read SERVER
do
        ssh -q -l login_name $SERVER 'nohup ksh /tmp/script.ksh -E' &
done < serverfile

# Wait for all of them to finish
wait

Try

ssh -qxn $i "ksh -c 'exec ksh /tmp/script.ksh >/dev/null 2>&1' &"
sleep 1

The -x closes X11 connection.
The -n closes stdin; stdout and stderr is closed at the remote side.
The ksh -c wrapper is to ensure that >/dev/null 2>&1 works even if login shell were a C-Shell.
The sleep 1 is best practice (allows to ^C interrupt, avoids problem with too fast socket allocation, ...)
I think nohup is not needed here.
-E option is not portable - specific to Linux/pdksh?

@MadeInGermany

-E option is needed as an argument for this script. Also,since i will be running the script over ssh is the "-x" option needed?

---------- Post updated at 12:21 PM ---------- Previous update was at 12:19 PM ----------

@Corona688

SERVER is the Serverlist file is guess, then what is "serverfile"?

SERVER is the name of the variable to read into, which is why $SERVER is used later.

serverlist is the server list file.

I'd actually make one change here:

ssh -n -q -l login_name $SERVER 'nohup ksh /tmp/script.ksh -E' &

Also, i want to invoke the script in background on the respective servers and then exit from there and then proceed to the next server on the list, i dont want the script to wait for finishing on one server and then proceed to the next.

My script does that. It doesn't run them in turn, it runs them in parallel.

It waits for all of them at once, afterwards.

My bad - then it's

ssh -qxn $i "ksh -c 'exec ksh /tmp/script.ksh -E >/dev/null 2>&1' &"
sleep 1

Without -x the remote side wants to connect to the calling host's X11 display. If it succeeds it will block ssh until it exits.

---------- Post updated at 12:41 PM ---------- Previous update was at 12:36 PM ----------

You can as well try

ssh -qxn $i "ksh -c 'exec ksh /tmp/script.ksh -E >/dev/null 2>&1'" &
sleep 1

This will be faster if a remote server is down.

---------- Post updated at 12:51 PM ---------- Previous update was at 12:41 PM ----------

@Corona688:
The following descriptor magic allows ssh without -n

while read SERVER <&3; do
 ssh ... # -n not needed
done 3< Server_List