问题描述
我试图在命令行中通过Plink打开SSH隧道以运行一些R脚本,但我无法运行R脚本,并且在它们运行时未创建隧道,因此与我的数据库的连接崩溃
I am trying to open an SSH tunnel via Plink in the command line to run a few R scripts and I cannot get the R scripts to run and when they do run the tunnel was not created, so the connection to my database crashes.
Plink代码如下:
The Plink code looks like this:
plink.exe -ssh [username]@[hostname] -L 3307:127.0.0.1:3306 -i "[SSH key]" -N
随后是运行R脚本的代码
Followed by the code to run the R scripts
"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R
我似乎无法让隧道保持打开状态以运行R脚本.但是,我可以单独打开隧道并运行代码.
I cannot seem to get the tunnel to stay open to run the R script. However, I can open the tunnel separately and run the code.
推荐答案
我假设您在批处理文件中有两个命令,一个接一个,如下:
I assume you have the two commands in a batch-file one after another like:
plink.exe -ssh [username]@[hostname] -L 3307:127.0.0.1:3306 -i "[SSH key]" -N
"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R
然后确实确实不会执行R.exe
,因为plink.exe
会无限期运行.
Then indeed the R.exe
is never executed as the plink.exe
runs indefinitely.
您必须并行运行命令:
- 您可以使用
start
命令在后台运行plink.exe
. - 在
R.exe
完成后,使用killtask
命令杀死后台plink
进程. - 您可能还应该在运行
R.exe
之前稍作停顿,以允许Plink建立隧道.
- You can use
start
command to runplink.exe
in the background. - Use
killtask
command to kill the backgroundplink
process after theR.exe
finishes. - You should probably also pause a little before you run
R.exe
to allow the Plink to establish the tunnel.
rem Open tunnel in the background
start plink.exe -ssh [username]@[hostname] -L 3307:127.0.0.1:3306 -i "[SSH key]" -N
rem Wait a second to let Plink establish the tunnel
timeout /t 1
rem Run the task using the tunnel
"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R
rem Kill the tunnel
taskkill /im plink.exe
这篇关于通过Plink打开SSH隧道,并通过命令行批处理文件运行R脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!