[comp.unix.questions] Getting rid of controlling terminal

pcb@gator.cacs.usl.edu (Peter C. Bahrs) (02/17/90)

I hope this is the correct newsgroup, so many unix., but...
I want to run a job in the background and have it disconnect from
the associating terminal without hardcoding it.
i.e. when I do a ps -guax on Berkeley I want to see a ? in the tty column.

So  program args >& file </dev/null  does not seem to work?
Any suggestions?  The problem is, on a SUN network anyway, if I use
the tty, then others cannot rlogin in to this machine (I rlogin'ed to invoke
the command).


/*------------Thanks in advance...---------------------------------------+
| Peter C. Bahrs                                                         |
| The USL-NASA Project                                                   |
| Center For Advanced Computer Studies   INET  pcb@gator.cacs.sl.edu     |
| University of Southwestern Louisiana   ...!uunet!dalsqnt!gator!pcb     |
| Lafayette, LA 70504                                                    |
+-----------------------------------------------------------------------*/

cpcahil@virtech.uucp (Conor P. Cahill) (02/18/90)

In article <4018@rouge.usl.edu> pcb@gator.cacs.usl.edu (Peter C. Bahrs) writes:
>So  program args >& file </dev/null  does not seem to work?

Try  "nohup program args >file  2>&1 &" from /bin/sh.

I believe the same thing will work for csh (substituting >&file for
">file 2>&1", of course)



-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

pemurray@miavx1.acs.muohio.edu (02/18/90)

In article <4018@rouge.usl.edu>, pcb@gator.cacs.usl.edu (Peter C. Bahrs) writes:
> I hope this is the correct newsgroup, so many unix., but...
> I want to run a job in the background and have it disconnect from
> the associating terminal without hardcoding it.
> i.e. when I do a ps -guax on Berkeley I want to see a ? in the tty column.
> 
> So  program args >& file </dev/null  does not seem to work?
> Any suggestions?  The problem is, on a SUN network anyway, if I use
> the tty, then others cannot rlogin in to this machine (I rlogin'ed to invoke
> the command).


From the syntax of your command, I think your using the c-shell.  To run a
process in the background, the only thing you need to do is put an '&' on
the end of your command.  For instance:

	program arcs >& file &

This process should continue even when you log out.

Peter

pat@rwing.UUCP (Pat Myrto {rwing}) (02/19/90)

In article <4018@rouge.usl.edu>, pcb@gator.cacs.usl.edu (Peter C. Bahrs) writes:
> I hope this is the correct newsgroup, so many unix., but...
> I want to run a job in the background and have it disconnect from
> the associating terminal without hardcoding it.
> i.e. when I do a ps -guax on Berkeley I want to see a ? in the tty column.

The following little ditty is what I use for System V (also puts the
program in the background).  With some changes to use the syscall for
setting a new process group under BSD, this should do what you have in
mind.  I'd be more specific, but I don't know BSD syscalls....

=========[ fsetpgrp.c - quick and dirty way to detach term assoc ]==========

/*
 * Fsetpgrp - a simple command that places programs listed on
 * the command line in the background, and disassociates them
 * with the terminal (by setting a new process group).  Note that
 * stdin, stdout, and stderr are NOT changed - if user wishes to
 * have those directed elsewhere, such as /dev/null, the user must
 * do this on the command line, as well.  This is just meant to
 * be a quick and dirty convenience.
 */

#include <stdio.h>

main(argc,argv,envp)
int argc;
char *argv[], *envp[];
{
	int pid;

	if (argc < 2) {
		fprintf(stderr,"Usage: %s program [argv ... ]\n",
			argv[0]);
		exit(1);
	}

	pid = fork();
	if(pid == -1) {
		fprintf(stderr, "fork failed.\n");
		exit(1);
	}

	else if(pid > 0) {	/* parent - just exit cleanly */
		exit(0);
	}
	else {
		setpgrp();		/* child. setpgrp and exec program */
		execvp(argv[1], &(argv[1]));
		perror(argv[0]);
		exit(1);
	}
}

============================[ end ]=================================
Hope this helps, or at least stimulates ideas for what you want.

-- 
pat@rwing                                       (Pat Myrto),  Seattle, WA
                            ...!uunet!pilchuck!rwing!pat
      ...!uw-beaver!uw-entropy!dataio!/
WISDOM:    "Travelling unarmed is like boating without a life jacket"