- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近在ssh上运行命令时遇到了一些有点奇怪的行为。我想听听以下行为的任何解释。
运行ssh localhost 'touch foobar &'
会按预期创建一个名为foobar
的文件:
[bob@server ~]$ ssh localhost 'touch foobar &'
[bob@server ~]$ ls foobar
foobar
-t
选项强制伪伪分配无法创建
foobar
:
[bob@server ~]$ ssh -t localhost 'touch foobar &'
Connection to localhost closed.
[bob@server ~]$ echo $?
0
[bob@server ~]$ ls foobar
ls: cannot access foobar: No such file or directory
[bob@pidora ~]$ ssh -t localhost 'touch foobar & sleep 1'
Connection to localhost closed.
[bob@pidora ~]$ ls foobar
foobar
最佳答案
哦,那是一个好人。
这与进程组的工作方式,使用-c
作为非交互式shell调用bash时的行为以及输入命令中&
的效果有关。
答案是假设您熟悉UNIX中的作业控制如何工作。如果不是这样,那么这是一个高级 View :每个进程都属于一个进程组(同一组中的进程通常作为命令管道的一部分放置在该组中,例如cat file | sort | grep 'word'
会将运行cat(1)
,sort(1)
和grep(1)
的进程放在同一进程组)。 bash
是一个与其他进程一样的进程,并且它也属于某个进程组。进程组是 session 的一部分(一个 session 由一个或多个进程组组成)。在一个 session 中,最多有一个进程组,称为前台进程组,可能还有许多后台进程组。前台进程组拥有对终端的控制权(如果 session 中连接了控制终端); session 负责人(bash)使用tcsetpgrp(3)
将进程从后台移动到前台,并从前台移动到后台。发送到进程组的信号将传递到该组中的每个进程。
如果您对流程组和作业控制的概念完全陌生,我认为您需要仔细阅读一下才能完全理解此答案。 UNIX环境中高级编程(第3版)的第9章是了解这一点的重要资源。
话虽如此,让我们看看这里发生了什么。我们必须把难题的每一部分都放在一起。
在这两种情况下,ssh远程端都使用bash(1)
调用-c
。 -c
标志使bash(1)
作为非交互式shell运行。从联机帮助页:
An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.
-t
在ssh上强制分配PTY,都会发生这种情况。&
的使用具有使 shell 程序不等待命令终止的副作用(即使禁用了作业控制)。从联机帮助页:If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.
touch(1)
将与bash(1)
在同一进程组中执行。setpgid(2)
联机帮助页:If a session has a controlling terminal, and the CLOCAL flag for that terminal is not set, and a terminal hangup occurs, then the session leader is sent a SIGHUP. If the session leader exits, then a SIGHUP signal will also be sent to each process in the foreground process group of the controlling terminal.
-t
时-t
时,在远程端没有PTY分配,因此bash并不是 session 领导者,并且实际上没有创建新的 session 。因为sshd作为守护程序运行,所以 fork + exec()的bash进程将没有控制终端。这样,即使shell终止非常快(可能在touch(1)
之前),也不会将SIGHUP
发送到进程组,因为bash不是 session 领导者(并且没有控制终端)。所以一切正常。-t
时-t
强制进行PTY分配,这意味着ssh远程将调用setsid(2)
,分配一个伪终端+用forkpty(3)
分配一个新进程,将PTY主设备的输入和输出连接到通向您计算机的套接字端点,最后执行bash(1)
。 forkpty(3)
在派生进程中打开PTY从属端,该分支将成为bash;由于当前 session 没有控制终端,并且终端设备正在打开,因此PTY设备成为该 session 的控制终端,bash成为 session 领导者。touch(1)
在同一个进程组中执行,例如yadda yadda。关键是这次是,有一个 session 负责人和一个控制终端。因此,由于bash不会因为&
而烦恼等待,因此退出时,SIGHUP
会传递到进程组,并且touch(1)
会过早死亡。nohup
nohup(1)
在这里不起作用,因为仍然存在竞争条件。如果bash(1)
在nohup(1)
有机会设置必要的信号处理和文件重定向之前终止,则它将无效(这可能会发生)set -m
做到这一点。这有效:ssh -t localhost 'set -m ; touch foobar &'
touch(1)
完成:ssh -t localhost 'touch foobar & wait `pgrep touch`'
关于bash - 如果分配了伪tty,为什么通过ssh运行后台任务失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32384148/
我用 IntelliJ IDEA 2021.1 CE 在 流行!_OS 20.04 与 bash 5.0.17 . 问题造句:我将IntelliJ终端设置为/bin/bash通过 IntelliJ 设
给定如下命令: bash --shortcuts 我想显示一个快捷方式列表,就像在这个页面上一样: http://www.skorks.com/2009/09/bash-shortcuts-for-m
我有一个脚本可以操作数据、创建参数并将它们发送到第二个脚本。其中一个参数包含一个空格。 脚本1.sh: args=() args+=("A") args+=("1 2") args+=("B") .
我的脚本的“只运行一次”版本的一个非常简单的示例: ./myscript.sh var1 "var2 with spaces" var3 #!/bin/bash echo $1 #output: va
我想了解数字( double )在 bash 中是如何表示的,以及当我在 bash 中以十六进制格式打印数字时会发生什么。 根据 IEEE 754 标准,double 应由 64 位表示:52 位(1
我试图在 bash -c "..." 命令中获取 bash 脚本,但它不起作用。 如果我在 bash -c "..." 之外运行命令,它会起作用。 我需要使用 bash -c "..." 因为我想确保
如何检测我的 bash shell 中是否加载了 bash 补全包?从 bash-completion 的 2.1 版(包含在 Debian 8 中)开始,除了 BASH_COMPLETION_COM
我的 bash_profile 中有一个投影函数。现在我试图从 bash 脚本中调用这个函数,但是我得到了一个未找到的错误。如何使投影函数对 bash 脚本可见? 最佳答案 必须导出函数 export
我正在编写一个 bash 脚本,它接受许多命令行参数(可能包括空格)并通过登录 shell 将它们全部传递给程序 (/bin/some_program)。从 bash 脚本调用的登录 shell 将取
当我创建一个新的 bash 进程时,提示符默认为一个非常简单的提示符。我知道我可以编辑 .bashrc 等来更改它,但是有没有办法使用 bash 命令传递提示? 谢谢! 最佳答案 提示由 PS1、PS
好的,我希望这个问题有一定道理,但是 bash shell 和 bash 终端之间有什么区别?例子。当我第一次打开终端时,会提示我当前的目录和用户名。在终端窗口标题中显示 -bash- ,当我键入 e
我是 SBCL 的新手,我正在尝试从 bash 终端运行存储在文本文件中的 Lisp 脚本。 这是我在文件开头写的内容 http://www.sbcl.org/manual/#Running-from
我知道我们可以在 bash 中使用将十六进制转换为十进制 #!/bin/bash echo "Type a hex number" read hexNum echo $(( 16#$hexNum ))
我正在尝试在 bash 脚本中自动完成文件夹名称。如果我输入完整的文件夹名称,一切正常,但我不知道如何自动完成名称。有什么想法吗? repo() { cd ~/Desktop/_REPOS/$1 }
我想检查远程网站上的一些文件。 这里是bash命令生成计算文件md5的命令 [root]# head -n 3 zrcpathAll | awk '{print $3}' | xargs -I {}
是否有任何内置函数可以使用 bash shell 脚本从给定日期获取下周日(下周一、下周二等)?例如,2014 年 9 月 1 日之后的第一个星期日是什么时候?我预计 2014 年 9 月 7 日。
我一直在尝试根据表格重命名一些特定文件,但没有成功。它要么重命名所有文件,要么给出错误。 该目录包含数百个以长条形码命名的文件,我只想重命名包含模式 _1_ 的文件。 例子 barcode_1_bar
bash 中有没有办法用变量的内容替换文本文件中的占位符? 例如,我想发送一封电子邮件通知,如下所示: Dear Foo, Alert: blah blah blah blah blah blah
我有一个 bash 脚本,它在某些字符串上附加了一个重音字符,导致它失败,我找不到这些字符在哪里或如何进入那里。 这是一些示例输出: mv: cannot move â/tmp/myapp.zipâ
这个问题在这里已经有了答案: How do I place stdout on edit line? (1 个回答) Can a bash script prepopulate the prompt
我是一名优秀的程序员,十分优秀!