Re: Find process id of background ssh?
Recently I was doing something entirely different and was badly bitten by solution to this problem: Following code: Output=`something ` Will not finished until program something is exited. shell in this case not used waitpid, but tried to read from the pipe, till something closed it's end. I avoided waiting to something by doing Output=`something 1/dev/null 21 ` But if one want wait to the background process to finsish this can be solution. Valery. --- Maxim Veksler [EMAIL PROTECTED] wrote: On Dec 26, 2007 5:13 PM, linux. il [EMAIL PROTECTED] wrote: Hi List. I'm writing a script to automate some system maintenance tasks, and I want to connect over SSH to several remote computers and do stuff on them. I'm using ssh -f to background ssh so I can run the same operation on multiple machines in parallel, otherwise it will be too slow - the maintenance job may take up to a few minutes to run and the script is not supposed to be fully automatic: a human is to monitor the process. But I don't want just to fire and forget the SSH processes - I want to exit from the script only when all the SSH processes have completed. I can do that by monitoring the process ids of the background SSH processes, if I could know them - which I'm having a difficult time detecting. I'm writing in bash, and optimally it would be something like this: for server in 1 2 ...; do ssh -f [EMAIL PROTECTED] 'run maintenance task' pids=$pids $(getSSHpid) done while kill -0 $pids 2/dev/null; do echo Waiting..; sleep 1; done but I didn't manage to find a way to get the process id of the ssh process after it goes to background, other the 'ps'ing for it. How can I go about doing this? -- Oded Sorry for OT, but some kind of distributed shell seems me more suitable for this task - http://www.linux-mag.com/microsites.php?site=business-class-hpcsid=buildp=4658 You might as well check http://www.csm.ornl.gov/torc/C3/ -- Cheers, Maxim Veksler Free as in Freedom - Do u GNU ? = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED] Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
Re: Find process id of background ssh?
On Mon, 2007-12-31 at 06:49 -0800, Valery Reznic wrote: Recently I was doing something entirely different and was badly bitten by solution to this problem: Following code: Output=`something ` Will not finished until program something is exited. shell in this case not used waitpid, but tried to read from the pipe, till something closed it's end. But if one want wait to the background process to finsish this can be solution. Its interesting - I wasn't aware of this issue (never tried something like this, but I think I would have understood what happens). The reason its not a solution to my problem is that I want to background all the processes before starting to wait on them - otherwise I wouldn't have backgrounded them in the first place. but... maybe... function startSSH() { host=$1 ssh [EMAIL PROTECTED] sleep 3; echo 'Done waiting in $host' } cmds= for host in 1 2; do cmds=$cmds ( startSSH ${host} ); done echo $(eval $cmds) /dev/null echo all done! This would have worked nicely - if not for the fact that like solutions based on getting the shell to background the ssh process, this one doesn't take into account that users might need to type in passwords. If I want to keep using bash then I need to use the (partial and race condition prone) pgrep solution. But porting this script to a different language, if I don't want to prompt the user for the passwords myself and to creat the ssh tunnels myself progrematically, how would I go about detecting the backgrounded ssh processes ? -- Oded = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
Re: Find process id of background ssh?
On Dec 26, 2007 5:13 PM, linux. il [EMAIL PROTECTED] wrote: Hi List. I'm writing a script to automate some system maintenance tasks, and I want to connect over SSH to several remote computers and do stuff on them. I'm using ssh -f to background ssh so I can run the same operation on multiple machines in parallel, otherwise it will be too slow - the maintenance job may take up to a few minutes to run and the script is not supposed to be fully automatic: a human is to monitor the process. But I don't want just to fire and forget the SSH processes - I want to exit from the script only when all the SSH processes have completed. I can do that by monitoring the process ids of the background SSH processes, if I could know them - which I'm having a difficult time detecting. I'm writing in bash, and optimally it would be something like this: for server in 1 2 ...; do ssh -f [EMAIL PROTECTED] 'run maintenance task' pids=$pids $(getSSHpid) done while kill -0 $pids 2/dev/null; do echo Waiting..; sleep 1; done but I didn't manage to find a way to get the process id of the ssh process after it goes to background, other the 'ps'ing for it. How can I go about doing this? -- Oded Sorry for OT, but some kind of distributed shell seems me more suitable for this task - http://www.linux-mag.com/microsites.php?site=business-class-hpcsid=buildp=4658 You might as well check http://www.csm.ornl.gov/torc/C3/ -- Cheers, Maxim Veksler Free as in Freedom - Do u GNU ? = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
Re: Find process id of background ssh?
Hi List. I'm writing a script to automate some system maintenance tasks, and I want to connect over SSH to several remote computers and do stuff on them. I'm using ssh -f to background ssh so I can run the same operation on multiple machines in parallel, otherwise it will be too slow - the maintenance job may take up to a few minutes to run and the script is not supposed to be fully automatic: a human is to monitor the process. But I don't want just to fire and forget the SSH processes - I want to exit from the script only when all the SSH processes have completed. I can do that by monitoring the process ids of the background SSH processes, if I could know them - which I'm having a difficult time detecting. I'm writing in bash, and optimally it would be something like this: for server in 1 2 ...; do ssh -f [EMAIL PROTECTED] 'run maintenance task' pids=$pids $(getSSHpid) done while kill -0 $pids 2/dev/null; do echo Waiting..; sleep 1; done but I didn't manage to find a way to get the process id of the ssh process after it goes to background, other the 'ps'ing for it. How can I go about doing this? -- Oded Sorry for OT, but some kind of distributed shell seems me more suitable for this task - http://www.linux-mag.com/microsites.php?site=business-class-hpcsid=buildp=4658 = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
Re: Find process id of background ssh?
On Wed, 26 Dec 2007 17:50:44 +0200, Oded Arbel wrote: On Wed, 2007-12-26 at 16:02 +0200, Tom Rosenfeld wrote: pgrep ssh |tail -1 they way I read it ... ... it looks like there will be a race condition here. Maybe I could filter on session id or process group id if I would know how to set it. Here is a little trick that will solve the race condition - create a unique name for the ssh you run. Example of use in bash: SSH=`which ssh`# locate ssh on PATH NSSH=my_ssh_$$ # unique ssh name (you can add $USER, time ...) TSSH=/tmp/$NSSH# abs name rm -f $TSSH# may be we have leftovers ln -s $SSH $TSSH # create it on /tmp $TSSH command parameters # run the ssh in BG rm -f $TSSH# remove symbolic link (not needed any more) PID=`pgrep $NSSH` # find pid of MY ssh Ehud. -- Ehud Karni Tel: +972-3-7966-561 /\ Mivtach - Simon Fax: +972-3-7966-667 \ / ASCII Ribbon Campaign Insurance agencies (USA) voice mail and X Against HTML Mail http://www.mvs.co.il FAX: 1-815-5509341 / \ GnuPG: 98EA398D http://www.keyserver.net/Better Safe Than Sorry = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
Re: Find process id of background ssh?
On Wed, 2007-12-26 at 15:24 +0200, Shachar Shemesh wrote: Oded Arbel wrote: I'm using ssh -f to background ssh so I can run the same operation on multiple machines in parallel for server in 1 2 ...; do ssh -f [EMAIL PROTECTED] 'run maintenance task' pids=$pids $(getSSHpid) done while kill -0 $pids 2/dev/null; do echo Waiting..; sleep 1; done Havn't checked it, but: for server in 1 2 ... do ssh [EMAIL PROTECTED] 'run maintenance task' done should work. In particular, running ssh with -f means that it backgrounds itself, and thus the shell does not keep track over its process number. On the other hand, running with (assuming you don't need to put in a password) The problem is that I might need to put in the passwords (users that expect to run these tasks will probably exchange keys with the servers, but other users should be able to put in passwords - I don't want to control that). I should have mentioned that. -- Oded = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
Re: Find process id of background ssh?
I have not tested this extensively, but you could try: pgrep ssh |tail -1 This will give you the PID of that last ssh process -tom On Dec 26, 2007 2:55 PM, Oded Arbel [EMAIL PROTECTED] wrote: Hi List. I'm writing a script to automate some system maintenance tasks, and I want to connect over SSH to several remote computers and do stuff on them. I'm using ssh -f to background ssh so I can run the same operation on multiple machines in parallel, otherwise it will be too slow - the maintenance job may take up to a few minutes to run and the script is not supposed to be fully automatic: a human is to monitor the process. But I don't want just to fire and forget the SSH processes - I want to exit from the script only when all the SSH processes have completed. I can do that by monitoring the process ids of the background SSH processes, if I could know them - which I'm having a difficult time detecting. I'm writing in bash, and optimally it would be something like this: for server in 1 2 ...; do ssh -f [EMAIL PROTECTED] 'run maintenance task' pids=$pids $(getSSHpid) done while kill -0 $pids 2/dev/null; do echo Waiting..; sleep 1; done but I didn't manage to find a way to get the process id of the ssh process after it goes to background, other the 'ps'ing for it. How can I go about doing this? -- Oded = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED] -- -tom 054-244-8025
Re: Find process id of background ssh?
Oded Arbel wrote: Hi List. I'm using ssh -f to background ssh so I can run the same operation on multiple machines in parallel for server in 1 2 ...; do ssh -f [EMAIL PROTECTED] 'run maintenance task' pids=$pids $(getSSHpid) done while kill -0 $pids 2/dev/null; do echo Waiting..; sleep 1; done Havn't checked it, but: for server in 1 2 ... do ssh [EMAIL PROTECTED] 'run maintenance task' done for process in `seq 1 numservers` do wait %$process done should work. In particular, running ssh with -f means that it backgrounds itself, and thus the shell does not keep track over its process number. On the other hand, running with (assuming you don't need to put in a password) means that %1, %2 etc. translate to the processes the shell launched. The above script (maybe with slightly different syntax) should launch all jobs concurrently, and exit only after the last of the jobs has exited. Shachar = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
Re: Find process id of background ssh?
I have not tested this extensively but you could try: On Dec 26, 2007 2:55 PM, Oded Arbel [EMAIL PROTECTED] wrote: Hi List. I'm writing a script to automate some system maintenance tasks, and I want to connect over SSH to several remote computers and do stuff on them. I'm using ssh -f to background ssh so I can run the same operation on multiple machines in parallel, otherwise it will be too slow - the maintenance job may take up to a few minutes to run and the script is not supposed to be fully automatic: a human is to monitor the process. But I don't want just to fire and forget the SSH processes - I want to exit from the script only when all the SSH processes have completed. I can do that by monitoring the process ids of the background SSH processes, if I could know them - which I'm having a difficult time detecting. I'm writing in bash, and optimally it would be something like this: for server in 1 2 ...; do ssh -f [EMAIL PROTECTED] 'run maintenance task' pids=$pids $(getSSHpid) done while kill -0 $pids 2/dev/null; do echo Waiting..; sleep 1; done but I didn't manage to find a way to get the process id of the ssh process after it goes to background, other the 'ps'ing for it. How can I go about doing this? -- Oded = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED] -- -tom 054-244-8025
Re: Find process id of background ssh?
On Wed, 2007-12-26 at 16:02 +0200, Tom Rosenfeld wrote: I have not tested this extensively, but you could try: pgrep ssh |tail -1 This will give you the PID of that last ssh process they way I read it it will give me the PID of the ssh with the highest process id, which may not be the last. pgrep -n ssh OTOH will give me the pid of the most recently started ssh process which is what I need, but it looks like there will be a race condition here. Maybe I could filter on session id or process group id if I would know how to set it. -- Oded = To unsubscribe, send mail to [EMAIL PROTECTED] with the word unsubscribe in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]