Re: How to detach from remote command?
- From: Robert Newson <get.lost@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 10 Jan 2009 14:24:43 GMT
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.
.
- Follow-Ups:
- Re: How to detach from remote command?
- From: root
- Re: How to detach from remote command?
- References:
- How to detach from remote command?
- From: root
- Re: How to detach from remote command?
- From: Robert Newson
- Re: How to detach from remote command?
- From: root
- Re: How to detach from remote command?
- From: Robert Newson
- Re: How to detach from remote command?
- From: root
- How to detach from remote command?
- Prev by Date: Re: How to detach from remote command?
- Next by Date: Re: How to detach from remote command?
- Previous by thread: Re: How to detach from remote command?
- Next by thread: Re: How to detach from remote command?
- Index(es):
Relevant Pages
|