Getting rid of controlling terminal

Pat Myrto {rwing} pat at rwing.UUCP
Mon Feb 19 06:58:41 AEST 1990


In article <4018 at rouge.usl.edu>, pcb at 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 at rwing                                       (Pat Myrto),  Seattle, WA
                            ...!uunet!pilchuck!rwing!pat
      ...!uw-beaver!uw-entropy!dataio!/
WISDOM:    "Travelling unarmed is like boating without a life jacket" 



More information about the Comp.unix.questions mailing list