Re: tkined with an X-Server

Peter da Silva (peter@baileynm.com)
Tue, 15 Oct 1996 09:13:57 -0500 (CDT)

Use this program to PROPERLY detach your session from the parent process
group. Nohup doesn't do enough.

#! /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 <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of shell archive."
# Contents: ReadMe Makefile detach.c
# Wrapped by peter@sonic.nmti.com on Mon Oct 10 16:42:02 1994
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'ReadMe' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'ReadMe'\"
else
echo shar: Extracting \"'ReadMe'\" \(1255 characters\)
sed "s/^X//" >'ReadMe' <<'END_OF_FILE'
DETACH -- when it absolutely has to run!
X
There are rude programs out there that *insist* on knowing when you've
hit interrupt or logged out. They don't care that you've backgrounded
them and nohupped them... as soon as that signal comes in, they're
gone.
X
Also, sometimes you have a background process run amok. You don't have
time to look it up, because it's printing junk all over your screen.
You have to 'kill -9 0'... but there's another background job there that
you don't want to kill.
X
Well, there is a solution... resting in /usr1/local/bin on xenix
is a new little program called 'detach'. Detach is really dumb,
but it does its job. What it does is run a command in a whole new
process group. Once you have detached a process, you can airmail
your terminal to Brazil. It won't care.
X
So, when you're going to background that 80 hour bind, detach it.
X
Usage: detach [-logfile] program
X
By default, detach redirects output to "detach.out". You can
override this with the '-logfile' option. It doesn't do any path
lookup or anything, so you'd be advised to do something like
X"detach /bin/sh -c command"...
X--
Peter da Silva `-_-' Ferranti International Controls Corporation.
X"Have you hugged U your wolf today?" sugar.uu.net!ficc!peter.
END_OF_FILE
if test 1255 -ne `wc -c <'ReadMe'`; then
echo shar: \"'ReadMe'\" unpacked with wrong size!
fi
# end of 'ReadMe'
fi
if test -f 'Makefile' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(357 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X# standard defines
SHELL=/bin/sh
CFLAGS=-O
SHARFILES=ReadMe Makefile $(MISC) $(CFILES)
SHAR=$(TARGET).shar
X
X# program dependent defines
OFILES=detach.o
CFILES=detach.c
TARGET=detach
MISC=
X
default: $(TARGET)
X
all: $(TARGET) shar
X
shar: $(SHAR)
X
X$(TARGET): $(OFILES)
X $(CC) $(CFLAGS) $(OFILES) -o $(TARGET)
X
X$(SHAR): $(SHARFILES)
X shar >$(SHAR) $(SHARFILES)
END_OF_FILE
if test 357 -ne `wc -c <'Makefile'`; then
echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'detach.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'detach.c'\"
else
echo shar: Extracting \"'detach.c'\" \(1116 characters\)
sed "s/^X//" >'detach.c' <<'END_OF_FILE'
X#include <stdio.h>
X#include <fcntl.h>
X
main(ac, av)
int ac;
char **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 }
next_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 setpgrp();
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_FILE
if test 1116 -ne `wc -c <'detach.c'`; then
echo shar: \"'detach.c'\" unpacked with wrong size!
fi
# end of 'detach.c'
fi
echo shar: End of shell archive.
exit 0