Skip to content

Instantly share code, notes, and snippets.

@groner
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groner/51acca4fb08d45582035 to your computer and use it in GitHub Desktop.
Save groner/51acca4fb08d45582035 to your computer and use it in GitHub Desktop.
newpgrp() {
# Non-interactive shells don't have job control. Spawn an interactive shell
# to create a new process group. The inner non-interactive shell is there
# to avoid interactive side-effects later on.
$BASH --norc -ic '$BASH -c \"\$@\" -- "$@"' -- "$@"
}
# Notes on things that don't work:
# set -i
# You can't turn job control on this way
# $BASH -is <<EOF
# This will try to run prompt commands from the environment and may create history entries or potentially even truncate the user's history file.
# This is sort of portable, right?
setsid() {
local setsid=$(mktemp -t setsid) exec
if test $1 = exec; then
exec=exec; shift
fi
cc -x c - -o $setsid <<-'EOF' && $exec $setsid "$@"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
unlink(argv[0]);
setsid();
execvp(argv[1], argv+1);
perror(argv[1]);
exit(1);
}
EOF
}
#!/usr/bin/env bash
if test "$1" = report; then
ps -o pid,pgid -p $$
else
. newpgrp.sh
echo plain
$BASH $0 report
echo -ic -c
newpgrp $BASH $0 report
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment