-1

I have 3 ec2 machines, and on each machines I have to run some server. So I am trying to create a bash script to accomplish the task. sample script

ssh -t ubuntu@server1 << EOL
  start server1
EOL

ssh ubuntu@server2 << EOL
  start server2
EOL

ssh ubuntu@server3 << EOL
  start server3
EOL

Now the problem is that after my script encounters the start Server1 command, it gets stuck there, as it is running a server, so it is pretty obvious.

I want to make sure that the whole script runs, i.e. server 1 and server 2 get started successfully, and I only see the server3 command output on my local machine's terminal.

Can anyone please give me a script, which will suffice my needs? I am a complete bash script noob. Kindly help

iron_man83
  • 67
  • 2
  • 9
  • Maybe try Ansible instead? And don’t write 3 separate commands. Use functions to re-use code. – Mike Doe Feb 19 '22 at 06:43
  • There's no `runServer1` command in the code you show. Don't type what you *think* is your code. Cut and paste actual code. – Jens Feb 19 '22 at 09:03
  • @Jens sorry for the confusion , corrected it in the question. I don't want to introduce any additional complexity, that is why I used pseudo code. For your reference , start server1 is actually ```locust``` command , which is basically used to start a server – iron_man83 Feb 19 '22 at 10:36

1 Answers1

0

How about:

ssh -t ubuntu@server1 "start server1" &
ssh ubuntu@server2 "start server2" &
ssh ubuntu@server3 "start server3" &
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • What happens when backgrounded processes produce output on stdout or stderr? – Jens Feb 19 '22 at 11:37
  • @Jens I'm not sure what you are getting at? Unwanted output can be discarded in the usual way by redirecting to `/dev/null` so I guess you're alluding to something I've missed? – Mark Setchell Feb 19 '22 at 11:43
  • Maybe I'm just misremembering, but couldn't the ssh be sent a SIGTTOU (22, background write attempted to controlling terminal) which would make everything appear to hang (until foregrounded)? Redirecting would of course get around this. – Jens Feb 19 '22 at 13:40