1

I have this code :

for i in $ipb $ipc $ipd; do 
    ssh -i ~/.ssh/'key' 'name'@${i} /bin/bash << EOF
    nohup ${install_dir}/redis-stable/src/redis-server ${install_dir}/${port}/node.conf &
EOF
    let "port++"
done

which should start redis servers on all 3 ip addresses. However it currently only executes the first command(on ipb). Note: if the nohup-ed command is input manually, once the Redis server is started with the command, the user must press enter to be able to type other commands. The problem is not redis-related, but most likely comes from how the script is written, so no need to know about redis for this one I think. Ssh is also working fine.

What must I change to make it run all 3 commands (one on each ip)?

Thank you for your time and feel free to indicate if you need more info.

9
  • 1
    Add echo $ipb $ipc $ipd before the loop, and echo done $i before the done. Report the output. – berndbausch Mar 24 at 1:31
  • @berndbausch the ip addresses are fine. this exact for loop is working fine in another script, connecting to all 3 nodes successfully. What I forgot to mention is that when I run the script it basically starts the server on ipb so I go kill the server(by ssh-ing to ipb and manually killing the process in another terminal) then it starts the server on ipc and so on. The for loop keeps going, starting servers if I manually kill the prvious server. – aveillon Mar 24 at 1:37
  • Basicall the first echo successfully shows the ip addresses, the second one isnt displayed as the terminal shows that the redis server on ipb started. I kill it manually it echoes ipb then starts server on ipc, and so on. finally, the second echo successfully echoes the ipd value – aveillon Mar 24 at 1:41
  • Sorry, but I don't understand. Your question says the for loop only runs once, but the comment says it keeps going. – berndbausch Mar 24 at 1:41
  • @berndbausch yes sorry its not clear. it runs only once because it is "stuck" on the process on the first redis server being started. I go and kill the process, and the for loop keeps going. But I want it to run all 3. The server is stuck on a sort of "listening to connections" message, if I type the command manually I have to press enter to be able to type commands again. – aveillon Mar 24 at 1:43
3

Explanation

You specified a command (/bin/bash) to ssh without -t, so there is no tty allocated on the remote side. Then two things happen:

  • nohup does not redirect standard streams (it would if there was a tty);

  • SSH server waits for eof before disconnecting, the local ssh keeps running.

    And eof happens when there's no file descriptor by any process open on the writing end of the pipe, which typically only happens when all the processes that didn't have their stdout redirected to something else are gone.

Redirecting the standard streams like you did in your answer makes the SSH server disconnect early.

3
  • well the redis cluster using all 3 servers works so I guess I got lucky (or I didnt fully undertand your answer)! thank you very much for your input, I'll try your solution. and the explanations were also highly appreciated – aveillon Mar 24 at 3:10
  • There is no "my solution". There is your solution. My answer tries to explain why your solution worked. – Kamil Maciorowski Mar 24 at 3:12
  • oh I see! thank you! – aveillon Mar 24 at 3:16
0

Found the issue, adding this at the end of the nohup line (right before the &)

> foo.out 2> foo.err < /dev/null

thanks to @berndbausch for pointing me to the right direction!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.