Path: utzoo!attcan!uunet!lll-winken!ncis.llnl.gov!ncis!helios.ee.lbl.gov!pasteur!ucbvax!agate!labrea!decwrl!megatest!bgregory From: bgregory@megatest.UUCP (Brian Gregory) Newsgroups: comp.unix.wizards Subject: Re: rsh backgrounding Keywords: rsh background daemon Message-ID: <1216@megatest.UUCP> Date: 19 Jan 89 00:36:28 GMT Reply-To: bgregory@megatest.UUCP (Brian Gregory) Organization: Megatest Corporation, San Jose, Ca Lines: 140 Here is a small program that solves the rsh backgrounding problem for me. This version runs on BSD. Essentially, it detaches a child process from its controlling environment. You may find that some of the detachment steps may be more than needed, but they're all here for completeness. To solve the specific problem, try rsh daemon [args] this will return immediately and rsh will exit, leaving only the detached process running on the remote machine. Enjoy, Brian. ...!sun!megatest!bgregory ----8<--------8<--------8<--------8<--------8<--------8<--------8<--------8<--- #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh daemon.c <<'END_OF_daemon.c' X#include X#include X#include X#include X#include X#include X X X/*ARGSUSED*/ Xmain (argc, argv, envp) Xint argc; Xchar *argv[]; Xchar *envp[]; X X{ X X extern int errno; X extern char *sys_errlist[]; X int pid; X X /* check for a program to run */ X if (argc < 2) X { X (void) fprintf (stderr, "usage: %s [args]\n", argv[0]); X exit (2); X } X X switch (pid = fork ()) X { X X /* failed */ case -1: X perror (argv[0]); X exit (1); X break; X X /* child */ case 0: X { X int fd; X int err = 0; X X errno = 0; X X /* no terminal-related job control signals */ X#ifdef SIGTTOU X err = (err << 1) | ((int) signal (SIGTTOU, SIG_IGN) == -1); X#endif X#ifdef SIGTTIN X err = (err << 1) | ((int) signal (SIGTTIN, SIG_IGN) == -1); X#endif X#ifdef SIGTSTP X err = (err << 1) | ((int) signal (SIGTSTP, SIG_IGN) == -1); X#endif X X /* detach from process group */ X err = (err << 1) | (setpgrp (0, (pid = getpid ())) == -1); X X /* bail on a controlling tty */ X if ((fd = open ("/dev/tty", O_RDWR)) >= 0) X { X err = (err << 1) | (ioctl (fd, TIOCNOTTY, (char *) 0) == -1); X err = (err << 1) | (close (fd) == -1); X } X X /* close all files */ X for (fd = 0; fd < getdtablesize (); fd++) X (void) close (fd); X X /* move off a possibly mounted file system */ X err = (err << 1) | (chdir ("/") == -1); X X /* no inherited umask */ X err = (err << 1) | (umask (0) == -1); X X /* check for errors before running program */ X err <<= 1; X if (err) X goto derr; X X /* run the program */ X err |= (execvp (argv[1], &argv[1]) == -1); X X /* process error */ X derr: X fd = errno; X *stderr = *fopen ("/dev/console", "w"); X (void) fprintf (stderr, "%s \"%s\" (%d): 0x%x, last %d (%s)\n", X argv[0], argv[1], pid, err, fd, sys_errlist[fd]); X exit (1); X X } X break; X X /* parent */ default: X (void) fprintf (stderr, "[%d]\n", pid); X exit (0); X break; X X } X} END_OF_daemon.c if test 1911 -ne `wc -c