我正在Mac(Mavericks)上通过由launchd控制的bash shell脚本运行autossh。不幸的是,它们以我设置的方式导致autossh生成其自身和ssh的数十个实例。最终,外壳似乎停止了,无法进行远程连接。如果我同时杀死了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
已启动的plist文件位于〜/ Library / LaunchAgents中:
<?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实例?
答案 0 :(得分:0)
很明显,不需要保持脚本运行状态,而是ssh。
同样,这就是CoordinatorLayout
的用途(在掉线时自动重新连接ssh连接)。
将autossh
设置为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
,因为我们有PATH
具有完整路径。
欢迎。