1

Okay I saw a lot of questions like this, but almost all have either theoretical answers which do clear the concept but I am still not able to put it up in code.

What I want to achieve using a bash script is, using a loop,

  1. ssh to servers mentioned in a file ( this is done )

  2. execute update and upgrade command on each of them.

  3. exit asa the command is issued but the commands should keep running on the server.

Currently, with a normal script to do so, I can ssh and update all machines but serially...that is only one machine at a time as ssh exits only after the completion of update and upgrade.( with op directed to /dev/null to avoid unnecessary op on screen)

I see options like nohup, screen/tmux to do so...but I don't get how to use any of these to achieve the above!

How can I implemented this?

4

Prepare update.sh script which will be run on machines. Run it inside screen with ssh:

ssh user@host "screen -a -d -m -l -U /usr/local/sbin/update.sh"

Profit. It will run update script in dettached screen session, which will end as soon as the update.sh end.

  • okay so i just add this in the while loop of shell script? – Nirmik Sep 28 '14 at 11:35
  • Yes, exactly. You can also fork ssh into background i.e. ssh user@host "[...]" & to make the update more parallel on all machines. – Scyld de Fraud Sep 28 '14 at 11:38
  • okay...and i could just keep the script in /root and do ./update.sh right? – Nirmik Sep 28 '14 at 12:29
  • You do not need root to run this script, but yes you can do it like so. Perhaps you would even run it from cron. – Scyld de Fraud Sep 28 '14 at 12:31
  • yeah, to avoid password requirements of sudo ive rather made ssh to root :P the environment is not any kind of production environment so its okay :D – Nirmik Sep 28 '14 at 12:32

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.