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.

ilkkachu
  • 138,973
  • 1
    Add echo $ipb $ipc $ipd before the loop, and echo done $i before the done. Report the output. – berndbausch Mar 24 '21 at 01: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 '21 at 01: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 '21 at 01: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 '21 at 01: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 '21 at 01:43
  • I am not sure what makes the ssh hang. One solution might be running the ssh commands in the background. I would also experiment with redirecting stdin of the redis installation command to /dev/null. – berndbausch Mar 24 '21 at 01:48
  • @berndbausch thank you for your input! for a reason I dont know when trying the redis install command manually directly in the node, </dev/null doesnt work even though pressing enter does... using ssh -s gives me subsystem request failed on channel 0 error. I'll most likely just start the servers manually. but once again, thank you very much for trying to solve the issue! – aveillon Mar 24 '21 at 02:06
  • If pressing Enter solves the problem, perhaps echo | nohup ... does as well. – berndbausch Mar 24 '21 at 02:07
  • @berndbausch doesnt either... EDIT: found the solution, also needed to redirect out and error. now its working fine. thank you so much for helping! – aveillon Mar 24 '21 at 02:09

2 Answers2

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.

  • 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 '21 at 03:10
  • There is no "my solution". There is your solution. My answer tries to explain why your solution worked. – Kamil Maciorowski Mar 24 '21 at 03:12
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!