Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing ssh command in a bash shell script within a loop [duplicate]

Tags:

bash

ssh

I'm trying to execute an ssh command within a a bash shell script that should perform the following: 1) ssh to host 2) execute command 3) print value of command 4) repeat steps 1 -3 5) exit bash shell script

I have set up password less entry to the remote host, added host key to remote host

I want to test the various states of the httpd process running on the remote host Within a text file, httpd_process.txt, I have:

/etc/init.d/httpd status (stop, start, restart)

I do the following in the script:

while read LINE
do
    echo "Httpd Request: $LINE"
    status=`$LINE`
    echo "Status: $status"
    sleep 5 # sleep so that next 
done < /path_name/httpd_process.txt

exit 0

I assumed that each time through the loop another input string is read from the input text file and the request is made to the remote host. However, what I experience is that after the first request the script terminates. Am I correct to assume that as the first request is sent it creates a child process and once that process completes my script completes and the next turn through the loop is not executed?

like image 481
Roland Avatar asked Dec 17 '22 13:12

Roland


1 Answers

ssh is consuming stdin. Pass it -n to prevent this.

like image 81
Ignacio Vazquez-Abrams Avatar answered Apr 19 '23 23:04

Ignacio Vazquez-Abrams