4

When a running process gives lots of stdout output throughout its lengthy running process, you don't want to kill it and rerun it. How can you not show the output? Thanks.

4

One approach could be to attach a debugger to the process and make it open stdout on /dev/null:

gdb --batch -ex 'call close(1)' -ex 'call open("/dev/null",2)' -p "$pid"
  • Interesting. (1) first Ctrl-z to suspend it before running gdb? (2) Is call a command in gdb? Are close and open also? – Tim Feb 24 '15 at 22:30
  • @Tim please see my answer . – 把友情留在无盐 Feb 25 '15 at 5:30
  • Does the 2 in the call to open signify O_RDWR? – user001 Jan 10 at 0:29
1

let me yank here the solution @jimmij pointed to , with minor modification .

(gdb) attach <pid>    
... 
(gdb) call open("/dev/null",O_WRONLY)
$1 = 3
(gdb) call dup2($1,fileno(stdout))
$2 = 1
(gdb) call close($1)
...
(gdb) detach
...

for those not familiar with gdb , "attach" "call" "detach" are gdb commands . get information with "help attach" inside a gdb prompt . and "open" "dup2" "close" are library functions . get information with "man 2 open" .

here O_WRONLY equals 1 and fileno(stdout) equals 1 . use literal values or gdb may complain lack of symbols .


and if we want to find a file descriptor already opened , we can .

$ cd /proc/<pid>/fd
$ for i in * ; do
          if [[ `readlink $i` == "/dev/null" ]]
                  fd=$i ; break
          fi
  done
-1

The following will prevent normal returns as well as error messages

2&>1 >/dev/null
  • The OP asks about an already running process. – Matteo Feb 25 '15 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.