简体   繁体   English

退出嵌套外壳后,ssh脚本命令丢失

[英]ssh script commands lost after exiting nested shell

I am executing a script through ssh like this: 我正在通过ssh执行脚本,如下所示:

ssh $USER@somehost 'bash -s' < ./myscript.sh

inside myscript.sh : myscript.sh

echo '1'
sudo -i -u svcacct   # use the service account
echo '2'
exit                 # last working command
echo '3'             
exit
echo '4'

Output: 输出:

shellA$ 1
shellB$ 2
shellA$

As you can see, the exit command, while properly exiting the inner session, prevents the subsequent script commands from running. 如您所见, exit命令在正确退出内部会话时会阻止后续脚本命令运行。 The ssh connection hangs (Ctrl-C exits). ssh连接挂起(Ctrl-C退出)。 How do I fix this? 我该如何解决?

A working implementation, emitting 1 , 2 and 3 , would look like: 一个工作的实施,发射123 ,将如下所示:

echo '1'
sudo -u svcacct -i bash -s <<'EOF-1'
echo '2'
EOF-1
echo '3'             
exit
echo '4'

Note that we're not depending on any program to exit leaving the file descriptor for stdin with any particular content queued-up to be read, but instead are providing the svcacct instance with only a very limited and specific subset of stdin from a distinct heredoc. 请注意,我们依赖任何程序退出而将stdin的文件描述符留有排队等待读取的任何特定内容,而是为svcacct实例提供了一个非常有限且特定的stdin子集(来自一个独特的Heredoc) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM