Re: How to detach from remote command?



root wrote:

....
You are right about my failure to grasp setpgid. What I wanted to do
was to disassociate the task from the shell that rsh brought up.
I have been different options on my machine then porting them over
to the remote machine. I thought I had something when I created a
program which merely copied the command line over to execlp, but
rand setsid() before the execlp. Something like this:

pid=fork();
if(pid==0){
setsid();
execlp(......);
}
exit(0);

Try something like this:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main (argc, argv)
int argc;
char **argv;
{
if (!fork()) /* if child process */
{
int fd;
if ((fd = open("/dev/null", O_RDONLY)) < 0) exit(1);
close(0);
dup2(fd, 0); /* stdin = /dev/null */
close(fd);
if ((fd = open("/dev/null", O_WRONLY)) < 0) exit(1);
close(1);
close(2);
dup2(fd, 1);
dup2(fd, 2); /* stdout/err = /dev/null */
close(fd);
/*
* if you don't need to supply stdin/stdout/stderr with anywhere
* you can replace the above from line "int fd;" (inclusive)
* with:
* close(0);
* close(1);
* close(2);
*
* But note that *nix opens to the lowest available file descriptor (AFAIK)
* and so stdin/out/err will be reopened by whatever files the program may
* open, so the above is probably better.
*/
argv++; /* shift args so that arg 0 = program to run */
execvp(*argv, argv);
}
exit(0);
}


....

Mefinx the problem is that the forked remote process is keeping open the "tty" (socket) of the rsh (not ssh?) process. Your forking program will need to close [and reopen] stdin, stdout, stderr (file descriptors 0-2) [to relevant byte streams - especially stdout/stderr if you want results back somehow, if no input/output is needed, reconnect to /dev/null].

Here's what I think is happening:
rsh brings up a shell on the remote machine
the shell forks to the command, but stays around until the command finishes
rsh keeps the connection open until the shell closes.

No, the forked remote process keeps the connection open as it has stdin/out/err all connected to the connection; when it finishes, it closes the streams and so the connection can then die.

I'm pretty sure now that I can't do what I want.

Using ssh & the above program I can remotely start a background process without waiting for it to terminate on the remote host. Try the above program and see what happens.

.



Relevant Pages

  • Re: SuSE 9.2 Login terminates immediately
    ... because it canīt make a rsh connection. ... connection like you describe show the same symptoms. ... > when we have to work on a remote machine we make the connection ... The login takes place but immediately falls ...
    (alt.os.linux.suse)
  • SuSE 9.2 Login terminates immediately
    ... when we have to work on a remote machine we make the connection ... for some historical reasons with rsh or telnet (we are comming ... The login takes place but immediately falls ... back displaying the message 'connection closed'. ...
    (alt.os.linux.suse)
  • Re: waitFor and return (very strange)
    ... > rsh started by command line or java.Must i set an hide variable? ... treated differently by the remote shell. ...
    (comp.lang.java.programmer)
  • Re: rsh shell
    ... | into their home directory,but they also need to download files. ... Trying to wrap chroot cages around rsh ... On OpenServer, rsh is *not* the Berkeley remote shell, it is the ... Restricting the shell ...
    (comp.unix.sco.misc)
  • Re: rsh shell
    ... Trying to wrap chroot cages around rsh ... is like keeping a pudding in a paper bag. ... On OpenServer, rsh is *not* the Berkeley remote shell, it is the ... Defining a user's login shell as a restricted shell is different from using a remote shell service. ...
    (comp.unix.sco.misc)