0

我在 Mac (Mavericks) 上通过由 launchd 控制的 bash shell 脚本运行 autossh。不幸的是,我设置的方式会导致 autossh 产生数十个自身和 ssh 的实例。最终,shell 似乎停止运行,远程连接变得不可能。如果我将 autossh 和 ssh 都杀掉,一切都会恢复正常。

这在主机上:

Axe:~ mnewman$ ps -A | grep -c -w ssh
41
Axe:~ mnewman$ ps -A | grep -c -w autossh
152

这在客户端:

MrMuscle:~ mnewman$ ssh -p19990 mnewman@localhost
Last login: Sat Jul  6 16:19:50 2019 from localhost
-bash: fork: Resource temporarily unavailable
-bash-3.2$

我花了很长时间才开始运行它。到目前为止,我还不知道如何弄清楚这里发生了什么。

外壳脚本:

#!/bin/bash

/opt/local/bin/autossh -f -M 0 -N -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -o ServerAliveCountMax=2 -R 19990:localhost:22 mnewman@korat.myddns.rocks -p 10000 

~/Library/LaunchAgents 中的launchd plist 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Debug</key>
    <false/>
    <key>Disabled</key>
    <false/>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
</string>
    </dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.mgnewman.autossh</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/mnewman/bin/autossh.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/Users/mnewman/Desktop/autossh.txt</string>
    <key>StandardOutPath</key>
    <string>/Users/mnewman/Desktop/autossh.txt</string>
    <key>WorkingDirectory</key>
    <string>/Users/mnewman/Documents</string>
</dict>
</plist>

我在这里做了什么来产生这么多 autossh 和 ssh 的实例?

4

1 回答 1

0

显然它不需要让脚本保持活动状态,而是 ssh。
同样,这是autossh用于(在断开时自动重新连接 ssh 连接)。

设置KeepAlive为,false
或者,如果仍然需要,您可以像下面这样微调它:

<key>KeepAlive</key>
<dict>
     <key>SuccessfulExit</key>
     <false/>
</dict>

此外,最好exit 0在脚本末尾添加一行。

最后,如果这是脚本的唯一目的,要运行, autossh请考虑添加autossh.launchd


plist文件autossh
(我没有要测试的环境,但它应该可以工作。)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Debug</key>
    <false/>
    <key>Disabled</key>
    <false/>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.mgnewman.autossh</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/local/bin/autossh</string>
        <string>-M</string>
        <string>0</string>
        <string>-N</string>
        <string>-o</string>
        <string>ExitOnForwardFailure=yes</string>
        <string>-o</string>
        <string>ServerAliveInterval=30</string>
        <string>-o</string>
        <string>ServerAliveCountMax=2</string>        
        <string>-R</string>
        <string>19990:localhost:22</string>
        <string>mnewman@korat.myddns.rocks</string>
        <string>-p</string>        
        <string>10000</string>        
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/Users/mnewman/Desktop/autossh.txt</string>
    <key>StandardOutPath</key>
    <string>/Users/mnewman/Desktop/autossh.txt</string>
    <key>WorkingDirectory</key>
    <string>/Users/mnewman/Documents</string>
</dict>
</plist>  

更改:作为程序参数与每个参数一起
放置,除了(在后台发送)不再需要。 省略了环境变量,就像我们使用完整路径一样。 autossh-f
PATHautossh

欢迎。

于 2019-07-06T13:38:26.383 回答