| Linux Forum Index » Linux Miscellaneous Topics » How to detach from remote command?... |
|
Page 1 of 1 |
|
| Author |
Message |
| root... |
Posted: Thu Jan 08, 2009 8:09 am |
|
|
|
Guest
|
I want to use rsh (or anything else) to send a command to a machine on
my lan, but I want to detach from that command so I don't have to wait
until the command finishes.
Any suggestions for a clean way to detach? |
|
|
| Back to top |
|
|
|
| jellybean stonerfish... |
Posted: Thu Jan 08, 2009 1:01 pm |
|
|
|
Guest
|
On Thu, 08 Jan 2009 12:09:17 +0000, root wrote:
Quote: I want to use rsh (or anything else) to send a command to a machine on
my lan, but I want to detach from that command so I don't have to wait
until the command finishes.
Any suggestions for a clean way to detach?
man screen |
|
|
| Back to top |
|
|
|
| Robert Newson... |
Posted: Thu Jan 08, 2009 3:00 pm |
|
|
|
Guest
|
root wrote:
Quote: I want to use rsh (or anything else) to send a command to a machine on
my lan, but I want to detach from that command so I don't have to wait
until the command finishes.
Any suggestions for a clean way to detach?
detach in what sense?
So you can reattach later to see how it's doing?
if so:
as elsewhere advised: man screen
or if you want to suspend the rsh: type: ~^Z
you can then 'fg' it later.
Or so that you can leave it running and kill the connection?
if so: possibly "nohup" is your friend. |
|
|
| Back to top |
|
|
|
| root... |
Posted: Fri Jan 09, 2009 12:18 am |
|
|
|
Guest
|
Robert Newson <get.lost at (no spam) bullet3.fsnet.oc.ku> wrote:
Quote: root wrote:
I want to use rsh (or anything else) to send a command to a machine on
my lan, but I want to detach from that command so I don't have to wait
until the command finishes.
Any suggestions for a clean way to detach?
detach in what sense?
So you can reattach later to see how it's doing?
if so:
as elsewhere advised: man screen
or if you want to suspend the rsh: type: ~^Z
you can then 'fg' it later.
Or so that you can leave it running and kill the connection?
if so: possibly "nohup" is your friend.
Suppose that every few minutes some machine on the lan
originates a task for a dedicated machine also on
the lan. The dedicated machine only deals with these
tasks. I can't have a user on each machine monitoring
the jobs.
shutdown is a task that detaches and continues. So:
rsh remote shutdown -h now
will return to the calling machine immediately while
the remote machine shuts down.
I could set up a daemon on the remote machine which
processes jobs in some queue. Other machines on the
lan would just stick an entry in the queue, they
wouldn't have to wait for the job to complete.
I was also thinking of a program on the remote machine
that does nothing but a fork to execute the task, but
uses setpgrp to "detach" from the forking program.
I know that last paragraph is obscure, let me explain.
Suppose I write a program vfork on the remote machine.
I call it like this:
rsh remote vfork command line to be executed
When vfork on the remote machine gets called
it sees its command line arguments as:
comand line to be executed. All vfork does
is execute those arguments via execlp. The
daughter process of vfork, however, is attached
to vfork so rsh waits for the daughter to finish.
So I want to use either setpgid or setpgrp to attach
the daughter process to another task, say the shell
under which some console is running.
My next step is to look into how shutdown does it. |
|
|
| Back to top |
|
|
|
| Robert Newson... |
Posted: Sat Jan 10, 2009 7:25 am |
|
|
|
Guest
|
root wrote:
....
Quote: I want to use rsh (or anything else) to send a command to a machine on
my lan, but I want to detach from that command so I don't have to wait
until the command finishes.
....
Suppose that every few minutes some machine on the lan
originates a task for a dedicated machine also on
the lan. The dedicated machine only deals with these
tasks. I can't have a user on each machine monitoring
the jobs.
You mean the task effectively works in the background on the remote machine,
ie you want to effectively do:
$ task &
but on the remote machine?
....
Quote: I was also thinking of a program on the remote machine
that does nothing but a fork to execute the task, but
uses setpgrp to "detach" from the forking program.
I know that last paragraph is obscure, let me explain.
Suppose I write a program vfork on the remote machine.
I call it like this:
rsh remote vfork command line to be executed
When vfork on the remote machine gets called
it sees its command line arguments as:
comand line to be executed. All vfork does
is execute those arguments via execlp. The
daughter process of vfork, however, is attached
to vfork so rsh waits for the daughter to finish.
So I want to use either setpgid or setpgrp to attach
the daughter process to another task, say the shell
under which some console is running.
I don't think setpgid() works in the way you think it does...it's use is in
the processing of signals and inputting of data from a terminal (see: man
setpgrp).
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]. |
|
|
| Back to top |
|
|
|
| root... |
Posted: Sat Jan 10, 2009 9:04 am |
|
|
|
Guest
|
Robert Newson <get.lost at (no spam) bullet3.fsnet.oc.ku> wrote:
Quote: You mean the task effectively works in the background on the remote machine,
ie you want to effectively do:
$ task &
but on the remote machine?
If I do
rsh remote sleep 100
the rsh keeps the connection open until the sleep command
terminates. It doesn't matter if I do:
rsh remote sleep 100 "&"
Quote:
I don't think setpgid() works in the way you think it does...it's use is in
the processing of signals and inputting of data from a terminal (see: man
setpgrp).
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);
Running this on my machine has the effect of running the command
in the background and the prompt comes back immediately. I later found
that there is a setsid command which does the same thing:
setsid command:
setsid sleep 20
comes right back with the sleep command running in the background.
No luck on the remote machine. rsh stays connected until the
command completes.
Quote:
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.
I'm pretty sure now that I can't do what I want. |
|
|
| Back to top |
|
|
|
| Robert Newson... |
Posted: Sat Jan 10, 2009 10:24 am |
|
|
|
Guest
|
root wrote:
....
Quote: 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);
}
....
Quote: 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.
Quote: 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. |
|
|
| Back to top |
|
|
|
| root... |
Posted: Sat Jan 10, 2009 1:11 pm |
|
|
|
Guest
|
Robert Newson <get.lost at (no spam) bullet3.fsnet.oc.ku> wrote:
Quote:
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.
Perfect. Your program detaches the command from rsh . Thanks for
the effort. |
|
|
| Back to top |
|
|
|
| Robert Newson... |
Posted: Sat Jan 10, 2009 3:02 pm |
|
|
|
Guest
|
root wrote:
....
Quote: Perfect. Your program detaches the command from rsh . Thanks for
the effort.
No effort at all, that'll be...
If could be improved, like checking the number of arguments passed is > 1 so
that if no args given, a help is given for example.
If you want to get really into it, you could extend it to allow
stdin/out/err be redirected to given files. |
|
|
| Back to top |
|
|
|
| Nitro... |
Posted: Sun Jan 11, 2009 10:06 am |
|
|
|
Guest
|
On Thu, 08 Jan 2009 19:00:16 GMT, Robert Newson wrote:
Quote:
root wrote:
I want to use rsh (or anything else) to send a command to a machine on
my lan, but I want to detach from that command so I don't have to wait
until the command finishes.
Any suggestions for a clean way to detach?
detach in what sense?
So you can reattach later to see how it's doing?
if so:
as elsewhere advised: man screen
or if you want to suspend the rsh: type: ~^Z
you can then 'fg' it later.
Or so that you can leave it running and kill the connection?
if so: possibly "nohup" is your friend.
I have used the at command with faverable results. at will spawn the command
as the user running the at command at the time specified. (just once or at
the same time every day etc). I have not found a nice way to monitor the
output if the program is not generating a log. You can redirect the output
to a log file and tail it? The status from at and the output can be mailed
to you opon completion of the command. You can pick a time to run a program
or run it now. try man at
I cannot remember the syntax but something like this
at now < /pgmpath/runme |
|
|
| Back to top |
|
|
|
| root... |
Posted: Mon Jan 12, 2009 3:15 am |
|
|
|
Guest
|
Nitro <nitro-57 at (no spam) no_spam_please_usa.net> wrote:
Quote: I have used the at command with faverable results. at will spawn the command
as the user running the at command at the time specified. (just once or at
the same time every day etc). I have not found a nice way to monitor the
output if the program is not generating a log. You can redirect the output
to a log file and tail it? The status from at and the output can be mailed
to you opon completion of the command. You can pick a time to run a program
or run it now. try man at
I cannot remember the syntax but something like this
at now < /pgmpath/runme
Thanks for the suggestion. Just now I played with the at command
and the syntax is unclear, with no examples in the man page.
One site suggested building the command interactively as:
at now
which works, but is not useful for me. I tried your syntax
as:
at now </usr/bin/sleep 20
but I can't get the argument 20 to the command in this form.
Quotation marks don't seem to clarify the command to at.
For commands that do not require arguments I see that I
can get by with your syntax, which would include executing
a script file. So, now I tried:
rsh remote at now "<"/pgmpath/runme
but I got:
warning: commands will be executed using /bin/sh
job 1 at Sun Jan 11 23:35:00 2009
but nothing started up.
I will keep playing with it if only to learn how to
use at.
Thanks again. |
|
|
| Back to top |
|
|
|
| Robert Newson... |
Posted: Mon Jan 12, 2009 2:35 pm |
|
|
|
Guest
|
root wrote:
....
Quote: I cannot remember the syntax but something like this
at now < /pgmpath/runme
Thanks for the suggestion. Just now I played with the at command
and the syntax is unclear, with no examples in the man page.
One site suggested building the command interactively as:
at now
....
Quote: which works, but is not useful for me. I tried your syntax
as:
at now </usr/bin/sleep 20
That says run the command:
at now 20
with stdin connected to /usr/bin/sleep (not very useful as /usr/bin/sleep is
a binary file)
....
Quote: a script file. So, now I tried:
rsh remote at now "<"/pgmpath/runme
That's escaped the shell redirect, so now you're trying to run:
at now "</pgmpath/runme"
which doesn't seem to work...not sure, but mefinx the stdin redirect is not
being interpreted on the remote host.
Assuming /pgmpath/runme is on the remote host, you should be able to do:
$ rsh remote at now -f /pgmpath/runme
if you have different commands, you can either set up a set of scripts, or
create it locally and then copy it to the remote host for execution. |
|
|
| Back to top |
|
|
|
| root... |
Posted: Mon Jan 12, 2009 3:27 pm |
|
|
|
Guest
|
Robert Newson <get.lost at (no spam) bullet3.fsnet.oc.ku> wrote:
Quote:
Assuming /pgmpath/runme is on the remote host, you should be able to do:
$ rsh remote at now -f /pgmpath/runme
if you have different commands, you can either set up a set of scripts, or
create it locally and then copy it to the remote host for execution.
Thanks for the guidance. |
|
|
| Back to top |
|
|
|
|