[comp.unix.shell] Remote shell question

smk@cbnews.cb.att.com (Stephen M. Kennedy) (02/01/91)

I want to start up a process in the background on a remote machine using
remsh (sysV) or rsh (bsd), e.g.,

	remsh/rsh remote-host 'sleep 30 > /dev/null 2>&1 &'

and I want the local remote shell command to return immediately.
This works if "remote-host" is a sysV machine, but if "remote-host"
is a SunOS 4.x (and perhaps this generalizes to bsd-like) machine,
the local remsh/rsh won't exit until the remote background command
terminates (or the remote rsh daemon is killed by root).  On the remote
side, ps'ing shows the sleep, a <defunct> process (the remote shell),
and the rshd (sleeping on "select" in the kernel).  What's going on
and is there a workaround?

Thanks,

Steve Kennedy
smk@cbosgd.att.com
att!cbosgd!smk

brad@bradley.bradley.edu (Bradley E. Smith) (02/01/91)

In <1991Feb1.125425.14866@cbnews.att.com> smk@cbnews.cb.att.com (Stephen M. Kennedy) writes:

>I want to start up a process in the background on a remote machine using
>remsh (sysV) or rsh (bsd), e.g.,

>	remsh/rsh remote-host 'sleep 30 > /dev/null 2>&1 &'
I had this same problem...what I did was to use a 'C' program below.

#include	<stdio.h>
main()
{
	int i;
	for (i=0;i<20;i++) {
		close(i);
	}
	i = fork();
	if(i) 
		exit(0);
	execl("/usr/openwin/demo/xterm","bradley", "-ls", "-sb", "-fn","9x15", 0);
}

byron@archone.tamu.edu (Byron Rakitzis) (02/02/91)

In article <1991Feb1.145242.16915@bradley.bradley.edu> brad@bradley.bradley.edu (Bradley E. Smith) writes:
>In <1991Feb1.125425.14866@cbnews.att.com> smk@cbnews.cb.att.com (Stephen M. Kennedy) writes:
>
>>I want to start up a process in the background on a remote machine using
>>remsh (sysV) or rsh (bsd), e.g.,
>
>I had this same problem...what I did was to use a 'C' program below.
>
>#include	<stdio.h>
>main()
>{
>	int i;
>	for (i=0;i<20;i++) {
>		close(i);
>	}
>	i = fork();
>	if(i) 
>		exit(0);
>	execl("/usr/openwin/demo/xterm","bradley", "-ls", "-sb", "-fn","9x15", 0);
>}


You don't really need to write a C program to close file-descriptors; any reasonable
shell like sh or rc can do this for you. I have two shell scripts, "rx" and "xr".
I type:
	rx goofy
and rx does an rsh to goofy, invoking "xr" with all the necessary arguments. "xr"
then takes care to close the stupid file-descriptors (I've found that going up to 9 is
sufficient), puts xterm in the background and exits. Here are "rx" and "xr" in their
entirety:

-------------

#!/usr/arch/bin/rc
if (! ~ $#* 1) {
        echo usage: rx hostname >[1=2]
        exit 1
}

# an rx from a local window will have DISPLAY set to unix:0.0; this must be changed.
switch ($DISPLAY) {
case unix:*
	~ $#hostname 0 && hostname=`hostname
        display=`{echo $DISPLAY | sed s/unix:/$hostname:/}

case *
        display=$DISPLAY

}

rsh $1 -n bin/sh/xr $display $1 >/dev/null &

-------------

#!/usr/arch/bin/rc -l

DISPLAY=$1

xterm -ls -n $2 -title $2 </dev/null >/dev/null >[2] /dev/null <[3=] <[4=] <[5=] <[6=] <[7=] <[8=] <[9=] &
# the redirections are a hack to make sure that the rshd knows it can exit

-------------

By the way, this "rx", "xr" setup is due to Paul Haahr, who originally wrote
these scripts in sh.

Byron.
--
Byron Rakitzis
byron@archone.tamu.edu

smk@cbnews.att.com (Stephen M. Kennedy) (02/02/91)

Looks like you went to the right college :-)

brad@bradley.bradley.edu (Bradley E. Smith) writes:
>>I want to start up a process in the background on a remote machine using
>>remsh (sysV) or rsh (bsd), e.g.,
>
>>	remsh/rsh remote-host 'sleep 30 > /dev/null 2>&1 &'
>I had this same problem...what I did was to use a 'C' program below.

Bingo!  This is the shell solution to my particular problem

	remsh/rsh remote-host 'sleep 30 <&- >&- 2>&- 7>&- &'

The offending fd #7 showed up in a "/etc/pstat -u pid-of-sleep-process" in
the file section.

Thank you, thank you, thank you!

Steve Kennedy
smk@cbosgd.att.com
att!cbosgd!smk

rbj@uunet.UU.NET (Root Boy Jim) (02/02/91)

?In <1991Feb1.125425.14866@cbnews.att.com> smk@cbnews.cb.att.com (Stephen M. Kennedy) writes:
?
?>I want to start up a process in the background on a remote machine using
?>remsh (sysV) or rsh (bsd), e.g.,
?
?>	remsh/rsh remote-host 'sleep 30 > /dev/null 2>&1 &'

Try the -n option to rsh.
-- 

	Root Boy Jim Cottrell <rbj@uunet.uu.net>
	Close the gap of the dark year in between

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) (02/06/91)

As quoted from <1991Feb1.170808.24481@cbnews.att.com> by smk@cbnews.att.com (Stephen M. Kennedy):
+---------------
| 	remsh/rsh remote-host 'sleep 30 <&- >&- 2>&- 7>&- &'
| 
| The offending fd #7 showed up in a "/etc/pstat -u pid-of-sleep-process" in
| the file section.
+---------------

*7* ????!  Was ist los?

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY

smk@cbnews.att.com (Stephen M. Kennedy) (02/07/91)

In article <1991Feb6.043021.7019@NCoast.ORG> allbery@ncoast.ORG (Brandon S. Allbery KB8JRR) writes:
>As quoted from <1991Feb1.170808.24481@cbnews.att.com> by smk@cbnews.att.com (Stephen M. Kennedy):
>+---------------
>| 	remsh/rsh remote-host 'sleep 30 <&- >&- 2>&- 7>&- &'
>| 
>| The offending fd #7 showed up in a "/etc/pstat -u pid-of-sleep-process" in
>| the file section.
>+---------------
>*7* ????!  Was ist los?

Good question.  It seems to be a dup of stderr.  Also under certain
conditions it shows up as fd #6, so now I just close every fd I can (0-9)
in the script to be safe.  I don't think it's a shell problem, unless it's
one shared by both /bin/sh and ksh.  Anybody know?

Steve Kenndy
smk@cbosgd.att.com

eric@mks.mks.com (Eric Gisin) (02/07/91)

>+---------------
>| 	remsh/rsh remote-host 'sleep 30 <&- >&- 2>&- 7>&- &'
>| 
>| The offending fd #7 showed up in a "/etc/pstat -u pid-of-sleep-process" in
>| the file section.
>+---------------
>*7* ????!  Was ist los?

This is a rshd bug. Csh seems to close fd 3-9 at some point.
This bug causes lots of wasted time when trying to write an xrsh
shell script for X11.  You try
	rsh host "DISPLAY=$DISPLAY $@ 0<>/dev/null 1<&0 2<&0 &"
but the rsh doesn't exit after starting the remote command.
You have to close fd 0-2 otherwise rshd will wait for them to be closed
by the background command ($@ &). But the socket has been dup'd to 6 or 7,
so you have to use
	rsh host "exec 3<&- 4<&- 5<&- 6<&- 7<&- 8<&- 9<&-; DISPLAY=..."