Skip to content

Instantly share code, notes, and snippets.

@treywelsh
Forked from zaius/background.sh
Last active April 23, 2018 20:38
Show Gist options
  • Save treywelsh/6d465e785b5728ca228ee829fb672c86 to your computer and use it in GitHub Desktop.
Save treywelsh/6d465e785b5728ca228ee829fb672c86 to your computer and use it in GitHub Desktop.
How to redirect a running process output to a file and log out
ctrl-z
bg
./fdredir.bash
# shell
disown
#!/usr/bin/env bash
#seems to be existing scripts: https://www.isi.edu/~yuri/dupx/
if [ $# - lt 1 ]; then
exho "USAGE: $0 PID" >&2
exit 1
fi
PID=$1
touch /tmp/stdout.$PID
&& echo "/tmp/stdout.$PID"
touch /tmp/stderr.$PID
&& echo "/tmp/stderr.$PID"
#reopen files descriptors to tmp files
gdb -nh \
-p $PID \
-batch \
-ex 'call dup2(open("/tmp/stdout.'$PID'", 1), 1)' \
-ex 'call dup2(open("/tmp/stderr.'$PID'", 1), 2)' \
-ex detach > /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment