bash child process
Hi folks,
I regularly run scripts that access up to 100 servers to modify, query, install or whatever. The script steps through a file containing the names of all the servers and then does its thing on one after another (using ssh sudo ...). It happens sometimes that a server does not respond. In that case the script usually hangs. I would like to run each server access as a kind of child process so that the script doesn't die just because one server out of a bunch did not respond. Maybe I should spawn child processes, keep track of the PID and kill them if they don't finish within a certain amount of time. Here is part of my script template: Code:
until [ $done ] Any ideas, sample code etc. would be welcome. Just BinWondrin :) |
I usually ping the remote server before attempting any operation:
Code:
if ping -c1 -W1 $servername > /dev/null |
Quote:
Also I think that loop could be rewritten as Code:
while read servername ; do |
Thanks for the replies.
Unfortunately I cannot ping the servers because most of them are firewalled and will only accept traffic on port 22. |
I tried that shorter cleaner loop but it only reads the 1st servername, executes the commands and exits.
Here is part of my echotime script. I wrote this initially to check if all servers are reachable before I run any other scripts. Kind of like the "ping" idea mentioned above. I modified the loop as you suggested but it reads only the first servername before it exits. What am I missing? Code:
# read til the end of the file |
Quote:
Code:
child() { function killSelf() { sleep ${sleep:=10s}; kill $$; }; killSelf & disown %1; $someCommand; } Quote:
Code:
nc -w 2 -z remoteHost 22 >/dev/null && doSomething || dont |
Hey UnSpawn,
I am really impressed by the "nc" option. This is a function that I was marginally aware of but it did not really occur to me to use it. It just wasn't part of my day-2-day toolkit. I love it! I checked out the man page for "nc" and I can already see all kinds of uses for it. So, thank you for pointing that one out to me. It will certainly be used. I have to wrap my mind around the child() function. I will try it out and if I can make it work it will be way quicker than what I am doing now. :cool: |
Quote:
Edit: Wow, I need to refresh more often. 3 posts before I hit submit! |
All of them GREAT ANSWERS.
I am really impressed with this forum! It is a very active forum and the quality of the replies is very high. I am very glad that I joined, folks. I hope I can pay back somehow. So thanks again for all of your answers. Got to go and improve a bunch of scripts! BinWondrin ( and now I know!) :D |
Quote:
Code:
ssh -n user@host |
All times are GMT -5. The time now is 11:18 AM. |