From peter@pobox.nmti.com  Thu Apr 16 07:47:08 1998
Received: from mail.baileynm.com (fw.baileynm.com [206.109.159.11])
          by hub.freebsd.org (8.8.8/8.8.8) with SMTP id HAA17023
          for <FreeBSD-gnats-submit@freebsd.org>; Thu, 16 Apr 1998 07:46:54 -0700 (PDT)
          (envelope-from peter@pobox.nmti.com)
Received: (qmail 11408 invoked from smtpd); 16 Apr 1998 14:46:30 -0000
Received: from unknown (HELO pobox.nmti.com) (root@198.178.0.191)
  by fw.nmti.com with SMTP; 16 Apr 1998 14:46:30 -0000
Received: (from root@localhost)
	by pobox.nmti.com (8.8.7/8.8.7) id JAA12754;
	Thu, 16 Apr 1998 09:46:29 -0500 (CDT)
	(envelope-from peter)
Message-Id: <199804161446.JAA12754@pobox.nmti.com>
Date: Thu, 16 Apr 1998 09:46:29 -0500 (CDT)
From: Peter da Silva <peter@baileynm.com>
Reply-To: peter@baileynm.com
To: FreeBSD-gnats-submit@freebsd.org
Subject: detach - nohup on steroids
X-Send-Pr-Version: 3.2

>Number:         6320
>Category:       misc
>Synopsis:       Sometimes nohup isn't good enough.
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    mike
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Apr 16 07:50:00 PDT 1998
>Closed-Date:    Mon Sep 02 11:13:54 PDT 2002
>Last-Modified:  Mon Sep 02 11:13:54 PDT 2002
>Originator:     Peter da Silva
>Release:        FreeBSD 2.2.5-RELEASE i386
>Organization:
Bailey Network Management
>Environment:

Any situation where you're kicking off a program to run as a daemon and
the induhvidual who wrote it decided that they really needed to trap
signals you've already set to be ignored.

>Description:

Normally programs that are going to run as a daemon double-fork themselves
to shed their parent, and setsid() to create a new process. Nohup doesn't
do this for you, so your nohupped process is still interacting with your
terminal.

>How-To-Repeat:

nohup intransigent-program &
logout
Watch it die screaming.

>Fix:
	
Detach completes the setsid/doublefork idiom for starting a daemon off.

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	Makefile
#	detach.1
#	detach.c
#
echo x - Makefile
sed 's/^X//' >Makefile << 'END-of-Makefile'
X# standard defines
XSHELL=/bin/sh
XCFLAGS=-O
XSHARFILES=Makefile $(MISC) $(CFILES)
XSHAR=$(TARGET).shar
X
X# program dependent defines
XOFILES=detach.o
XCFILES=detach.c
XTARGET=detach
XMISC=detach.1
X
Xdefault: $(TARGET)
X
Xall: $(TARGET) shar
X
Xshar: $(SHAR)
X
X$(TARGET): $(OFILES)
X	$(CC) $(CFLAGS) $(OFILES) -o $(TARGET)
X
X$(SHAR): $(SHARFILES)
X	shar >$(SHAR) $(SHARFILES)
END-of-Makefile
echo x - detach.1
sed 's/^X//' >detach.1 << 'END-of-detach.1'
X.TH DETACH 1
X.SH NAME
Xdetach \- when it absolutely has to run!
X.SH SYNOPSIS
X.B detach
X[-va] [ -f
X.I filename
X]
X.I command
X[
X.I args
X]
X.SH DESCRIPTION
X.B detach.
Xruns a command in a whole new
Xprocess group. Once you have detached a process, you can airmail
Xyour terminal to Brazil. It won't care.
X.SH OPTIONS
X.IP -v
XNatter at you about the command it's running, both on the terminal and in the
Xlog file.
X.IP "-f \fIfilename\fR"
XBy default, detach redirects output to "detach.out". You can
Xoverride this with the '-f' option. Really important if you
Xdon't have write access in the current directory.
X.IP -a
XAppend to the logfile instead of overwriting it.
X.SH AUTHOR
XPeter da Silva
X.SH NOTES
X.B Detach
Xuses
X.B execvp 
Xto execute the command. If you want to detach multiple commands
Xuse \fBdetach \fIoptions\fB sh -c\fR '\fIcommand\fR'...
END-of-detach.1
echo x - detach.c
sed 's/^X//' >detach.c << 'END-of-detach.c'
X#include <stdio.h>
X#include <fcntl.h>
X
Xmain(ac, av)
Xint ac;
Xchar **av;
X{
X	int pid;
X	char *file;
X	int vflag;
X	int mode;
X
X	close(0); open("/dev/null", 0);
X	close(1);
X
X	file = "detach.out";
X	mode = O_TRUNC;
X	vflag = 0;
X
X	while(**++av == '-') {
X		while(*++*av) {
X			switch(**av) {
X				case 'f':
X					if(*++*av)
X						file = *av;
X					else
X						file = *++av;
X					goto next_arg;
X				case 'v':
X					vflag++;
X					break;
X				case 'a':
X					mode = O_APPEND;
X					break;
X			}
X		}
Xnext_arg:	;
X	}
X
X	if(open(file, O_WRONLY|mode|O_CREAT, 0666) < 0) {
X		perror(file);
X		exit(1);
X	}
X
X	switch(pid = fork()) {
X		case -1:
X			perror(av[0]);
X			exit(1);
X			break;
X		case 0:
X			if(vflag) {
X				char **p = av;
X
X				printf("# %d", getpid());
X				while(*p)
X					printf(" %s", *p++);
X				putchar('\n');
X			}
X			fflush(stdout);
X			close(2); dup(1);
X			setsid();
X			execv(av[0], av);
X			execvp(av[0], av);
X			perror(av[0]);
X			exit(1);
X			break;
X		default:
X			if(vflag) {
X				fprintf(stderr, "# %d", pid);
X				while(*av)
X					fprintf(stderr, " %s", *av++);
X				fputc('\n', stderr);
X			} else
X				fprintf(stderr, "%d\n", pid);
X			break;
X	}
X}
END-of-detach.c
exit

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: mike 
State-Changed-When: Wed Aug 22 20:25:13 PDT 2001 
State-Changed-Why:  

As far as I can tell, the proposed system utility is a duplicate of 
daemon(3). 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=6320 

From: peter@taronga.com
To: freebsd-gnats-submit@FreeBSD.org, peter@baileynm.com
Cc: peter@taronga.com
Subject: Re: misc/6320: Sometimes nohup isn't good enough.
Date: 23 Aug 2001 10:34:27 -0000

 Not exactly. If there was a daemon(1) it might be a duplicate of that, but
 not only isn't there a daemon(1), detach(1) predates daemon(3), 4.4BSD, and
 probably 4.3BSD... I first wrote it for Xenix-286 around 1985 to 1987. Which
 is why it's not using daemon(3) internally.
 
 I suppose you could write a daemon(1), or rewrite detach(1) to use daemon(3),
 but it's code in hand. Nobody has to rewrite anything. And it works and it's
 portable (it runs on Xenix-286, Zeus, SunOS 4, System V, Solaris, SCO, Tru64,
 and has done so for the past 15 years).
 
 A standalone daemonizing program is something that is needed, especially as
 we get more and more non-open-source software drifting into FreeBSDland
 both directly and via things like the Linux emulator. If you don't just
 grab this (again, as code in hand) at least whack out a workalike... though
 I don't think all the options of detach(1) can be implemented via daemon(1),
 and each one has been added as a direct result of user demand over the past
 15 years.
 
State-Changed-From-To: closed->open 
State-Changed-By: mike 
State-Changed-When: Thu Aug 23 09:37:55 PDT 2001 
State-Changed-Why:  

Although this functionality is available through the daemon(3) function, 
there is no equivalent userland utility. 


Responsible-Changed-From-To: freebsd-bugs->mike 
Responsible-Changed-By: mike 
Responsible-Changed-When: Thu Aug 23 09:37:55 PDT 2001 
Responsible-Changed-Why:  

I'll look into implementing this. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=6320 
State-Changed-From-To: open->analyzed 
State-Changed-By: mike 
State-Changed-When: Mon Sep 3 11:06:57 PDT 2001 
State-Changed-Why:  

The daemon(8) utility from BSD/OS has been imported into -CURRENT. 
This will be MFC'd after 4.4-RELEASE. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=6320 

From: Peter Avalos <pavalos@theshell.com>
To: freebsd-gnats-submit@FreeBSD.org
Cc: mike@FreeBSD.org
Subject: Re: misc/6320: Sometimes nohup isn't good enough.
Date: Sun, 21 Apr 2002 00:15:30 -0700

 This has been in CURRENT for about 7 months, and I think -STABLE
 could benefit from this utility.
State-Changed-From-To: analyzed->closed 
State-Changed-By: mike 
State-Changed-When: Mon Sep 2 11:13:01 PDT 2002 
State-Changed-Why:  

daemon(8) has been MFC'd and will be in the next 4.x-RELEASE. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=6320 
>Unformatted:
