使用命令行重定向进程启动后的 STDERR/STDOUT? [英] Redirect STDERR / STDOUT of a process AFTER it's been started, using command line?

查看:26
本文介绍了使用命令行重定向进程启动后的 STDERR/STDOUT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shell中可以做重定向,> <等,但是程序启动后呢?

In the shell you can do redirection, > <, etc., but how about AFTER a program is started?

我是这样问这个问题的,在我的终端后台运行的程序不断输出烦人的文本.这是一个重要的过程,所以我必须打开另一个外壳以避免文本.我希望能够 >/dev/null 或其他一些重定向,以便我可以继续在同一个 shell 中工作.

Here's how I came to ask this question, a program running in the background of my terminal keeps outputting annoying text. It's an important process so I have to open another shell to avoid the text. I'd like to be able to >/dev/null or some other redirection so I can keep working in the same shell.

推荐答案

短时间关闭和重新打开您的 tty(即注销并重新登录,这也可能会终止进程中的某些后台进程)您只有一种选择左:

Short of closing and reopening your tty (i.e. logging off and back on, which may also terminate some of your background processes in the process) you only have one choice left:

  • 使用 gdb 附加到有问题的进程,然后运行:
    • p dup2(open("/dev/null", 0), 1)
    • p dup2(open("/dev/null", 0), 2)
    • 分离
    • 退出

    例如:

    $ tail -f /var/log/lastlog &
    [1] 5636
    
    $ ls -l /proc/5636/fd
    total 0
    lrwx------ 1 myuser myuser 64 Feb 27 07:36 0 -> /dev/pts/0
    lrwx------ 1 myuser myuser 64 Feb 27 07:36 1 -> /dev/pts/0
    lrwx------ 1 myuser myuser 64 Feb 27 07:36 2 -> /dev/pts/0
    lr-x------ 1 myuser myuser 64 Feb 27 07:36 3 -> /var/log/lastlog
    
    $ gdb -p 5636
    GNU gdb 6.8-debian
    Copyright (C) 2008 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    Attaching to process 5636
    Reading symbols from /usr/bin/tail...(no debugging symbols found)...done.
    Reading symbols from /lib/librt.so.1...(no debugging symbols found)...done.
    Loaded symbols for /lib/librt.so.1
    Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
    Loaded symbols for /lib/libc.so.6
    Reading symbols from /lib/libpthread.so.0...(no debugging symbols found)...done.
    [Thread debugging using libthread_db enabled]
    [New Thread 0x7f3c8f5a66e0 (LWP 5636)]
    Loaded symbols for /lib/libpthread.so.0
    Reading symbols from /lib/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
    Loaded symbols for /lib64/ld-linux-x86-64.so.2
    
    (no debugging symbols found)
    0x00007f3c8eec7b50 in nanosleep () from /lib/libc.so.6
    
    (gdb) p dup2(open("/dev/null",0),1)
    [Switching to Thread 0x7f3c8f5a66e0 (LWP 5636)]
    $1 = 1
    
    (gdb) p dup2(open("/dev/null",0),2)
    $2 = 2
    
    (gdb) detach
    Detaching from program: /usr/bin/tail, process 5636
    
    (gdb) quit
    
    $ ls -l /proc/5636/fd
    total 0
    lrwx------ 1 myuser myuser 64 Feb 27 07:36 0 -> /dev/pts/0
    lrwx------ 1 myuser myuser 64 Feb 27 07:36 1 -> /dev/null
    lrwx------ 1 myuser myuser 64 Feb 27 07:36 2 -> /dev/null
    lr-x------ 1 myuser myuser 64 Feb 27 07:36 3 -> /var/log/lastlog
    lr-x------ 1 myuser myuser 64 Feb 27 07:36 4 -> /dev/null
    lr-x------ 1 myuser myuser 64 Feb 27 07:36 5 -> /dev/null
    

    您还可以考虑:

    • 使用screen;screen 提供了多个虚拟 TTY,您无需打开新的 SSH/telnet/etc、会话即可在它们之间切换
    • 使用nohup;这使您可以关闭并重新打开会话,而不会丢失...进程中的任何后台进程.
    • using screen; screen provides several virtual TTYs you can switch between without having to open new SSH/telnet/etc, sessions
    • using nohup; this allows you to close and reopen your session without losing any background processes in the... process.

    这篇关于使用命令行重定向进程启动后的 STDERR/STDOUT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆