【问题标题】:Why does autossh spawn dozens of instances of itself and ssh?为什么 autossh 会产生几十个自身和 ssh 的实例?
【发布时间】:2019-11-16 16:02:57
【问题描述】:

我在 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$

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

shell脚本:

#!/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 实例?

【问题讨论】:

    标签: bash ssh osx-mavericks launchd autossh


    【解决方案1】:

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

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

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

    另外,最好在脚本末尾添加行exit 0

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


    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(在后台发送)。
    省略了环境变量PATH,因为我们有带有完整路径的autossh

    欢迎。

    【讨论】:

    • 谢谢。我不明白您所说的“在 launchd 中添加 autossh”是什么意思。
    猜你喜欢
    • 2012-03-16
    • 2023-03-26
    • 2023-04-09
    • 2017-12-18
    • 2018-08-15
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多