suspended(tty input)

后台程序读取终端将进入暂停状态

需要从STDIN读取输入的程序,后台运行时出行 suspended(tty input) 状态,在后台处于暂停工作状态。

  • 演示:
    $cat cread.sh
    while read foo; do

    echo Foo is $foo
    

    done
    $ ./cread.sh
    this
    Foo is this
    ^C
    ~/work/shell choe@MBP⌚ 14:20:11
    $ ./cread.sh &
    [2] 2357
    [2] + 2357 suspended (tty input) ./cread.sh
    ~/work/shell choe@MBP⌚ 14:20:20
    $ fg
    [2] - 2357 continued ./cread.sh

    Foo is
    ~/work/shell choe@MBP⌚ 14:20:56

    引入 STDIN redirect 功能

    $ ./cread.sh < /dev/null &
    [2] 2368
    [2] - 2368 done ./cread.sh < /dev/null
    ~/work/shell choe@MBP⌚ 14:21:13
    $ ps -ef|grep cread

    501  2346   564   0  2:20下午 ttys005    0:00.00 sh ./cread.sh
    

    ssh dynamic forward 后台运行

    $ ssh -T -D 1080 root@192.168.66.100 </dev/null &
    [1] 2665
    ~/blog on  master! choe@MBP⌚ 14:33:12
    $ Last failed login: Fri Nov 24 06:33:09 UTC 2017 from 192.168.145.21 on ssh:notty
    There were 4667 failed login attempts since the last successful login.

    [1] + 2665 done ssh -T -D 1080 root@192.168.66.100 < /dev/null
    加入STDIN重定向</dev/null 后,然而ssh 自动退出了,
    ~/blog on  master! choe@MBP⌚ 14:33:16
    $ ps -ef|grep ssh

    501  1426     1   0 10:56上午 ??         0:00.05 /usr/bin/ssh-agent -l
    

    ~/blog on  master! choe@MBP⌚ 14:38:37
    $ ssh -TnN -D 1080 root@192.168.66.100 &
    [1] 2706
    ~/blog on  master! choe@MBP⌚ 14:38:57
    $ ps -ef|grep ssh

    501  1426     1   0 10:56上午 ??         0:00.05 /usr/bin/ssh-agent -l
    501  2706   499   0  2:38下午 ttys001    0:00.03 ssh -TnN -D 1080 root@192.168.66.100
    

    ~/blog on  master! choe@MBP⌚ 14:39:09

If a background process tries to read from the terminal, it will be stopped. This is normal terminal and shell behavior. Only foreground processes are allowed to control the terminal like that.

To prevent it from reading from the terminal, run it with nohup to prevent it from accessing the terminal. This will redirect stdout/stderr automatically, but NOT redirect stdin , while stdout and stderr will be saved to a file in the current directory.

man hup
NAME
nohup – invoke a utility immune to hangups

SYNOPSIS
nohup [–] utility [arguments]

DESCRIPTION
The nohup utility invokes utility with its arguments and at this time sets the signal SIGHUP to
be ignored. If the standard output is a terminal, the standard output is appended to the file
nohup.out in the current directory. If standard error is a terminal, it is directed to the same
place as the standard output.

Some shells may provide a builtin nohup command which is similar or identical to this utility. Consult the builtin(1) manual page.

$ ps -ef|grep ssh
  501  1426     1   0 10:56上午 ??         0:00.05 /usr/bin/ssh-agent -l
  501  2706   499   0  2:38下午 ttys001    0:00.08 ssh -TnN -D 1080 root@192.168.66.40
  501  2863   499   0  3:04下午 ttys001    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn ssh
~/blog on  master! choe@MBP⌚ 15:04:07
$ kill 2706
[1]  + 2706 done       ssh -TnN -D 1080 root@192.168.66.40
~/blog on  master! choe@MBP⌚ 15:04:15
$ kill 2706
kill: kill 2706 failed: no such process
~/blog on  master! choe@MBP⌚ 15:04:22
$ ps -ef|grep ssh
  501  1426     1   0 10:56上午 ??         0:00.05 /usr/bin/ssh-agent -l
  501  2893   499   0  3:04下午 ttys001    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn ssh
~/blog on  master! choe@MBP⌚ 15:04:24

nohup+ssh+& 可正常工作, 但是在随后的命令行提示后,回车下出再suspended,可推测nohup并没有重定向stdin

$ nohup ssh -T -D 1080 root@192.168.66.40 &
[1] 3101
appending output to nohup.out
~/blog on  master! choe@MBP⌚ 15:17:54
$此处回车后
[1]  + 3101 suspended (tty input)  nohup ssh -T -D 1080 root@192.168.66.40
~/blog on  master! choe@MBP⌚ 15:18:15
$
~/blog on  master! choe@MBP⌚ 15:18:16
~/blog on  master! choe@MBP⌚ 15:18:16
$ ps -ef|grep ssh
  501  1426     1   0 10:56上午 ??         0:00.05 /usr/bin/ssh-agent -l
  501  3101   499   0  3:17下午 ttys001    0:00.05 ssh -T -D 1080 root@192.168.66.40
  501  3138   499   0  3:21下午 ttys001    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn ssh

结论:

  1. 后台程序读取终端输入导致程序暂停
  2. < /dev/null可 重定向 stdin
  3. nohup app 的作用
  • 使程序忽略sighup信号
  • 仅重定向 stdout/stderr,不重定向stdin