我正在尝试将一个非常耗时的脚本从php端点调用到运行matlab的服务器。
我决定使用phpseclib,因为很多人推荐它的便携性。我开始怀疑自己是否做出了正确的决定。也许你可以帮忙!
我遇到的问题是,无论我如何运行以下命令,它总是在完成之前返回true。
$read = $sftp->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX);
$sftp->write("matlab -nodisplay -nodesktop -nosplash -logfile remote_matlab.out -r \"cd('/some_supporting_files'); some_function('/some_data_I_just_uploaded.txt'); exit;\"\r\n");
$read2 = $sftp->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX);
我也尝试过这样运行:
$sftp->exec("matlab -nodisplay -nodesktop -nosplash -logfile remote_matlab.out -r \"cd('/some_supporting_files'); some_function('/some_data_I_just_uploaded.txt'); exit;\"");
我已经测试了一切(例如文件在那里,它没有出错等)它正确启动,但只是没有完成。我需要知道它什么时候结束,因为我需要下载它创建的文件。
如果我添加一个$sftp->setTimeout(40)
它每次都有效,但是如果不需要,我会讨厌有人等待那么久,我也无法保证脚本会在那时再回来。
一旦我开始工作,我仍然需要从Angular ping这个脚本。
这是我得到的日志:
"log": [
"<-",
"->",
"<- NET_SSH2_MSG_KEXINIT (since last: 0.158, network: 0.038s)",
"-> NET_SSH2_MSG_KEXINIT (since last: 0.001, network: 0s)",
"-> NET_SSH2_MSG_KEXDH_INIT (since last: 0.453, network: 0s)",
"<- NET_SSH2_MSG_KEXDH_REPLY (since last: 0.038, network: 0.038s)",
"-> NET_SSH2_MSG_NEWKEYS (since last: 0.457, network: 0s)",
"<- NET_SSH2_MSG_NEWKEYS (since last: 0, network: 0s)",
"-> NET_SSH2_MSG_SERVICE_REQUEST (since last: 0.006, network: 0s)",
"<- NET_SSH2_MSG_SERVICE_ACCEPT (since last: 0.105, network: 0.105s)",
"-> NET_SSH2_MSG_USERAUTH_REQUEST (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_USERAUTH_SUCCESS (since last: 0.071, network: 0.071s)",
"-> NET_SSH2_MSG_CHANNEL_OPEN (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION (since last: 0.169, network: 0s)",
"-> NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST (since last: 0.043, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_SUCCESS (since last: 0, network: 0s)",
"-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.036, network: 0.036s)",
"-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.037, network: 0.037s)",
"-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.036, network: 0.036s)",
"-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.037, network: 0.036s)",
"-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.034, network: 0.034s)",
"-> NET_SSH2_MSG_CHANNEL_OPEN (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION (since last: 0.033, network: 0.033s)",
"-> NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_SUCCESS (since last: 0.041, network: 0.041s)",
"-> NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST (since last: 0.035, network: 0.035s)",
"<- NET_SSH2_MSG_CHANNEL_SUCCESS (since last: 0, network: 0s)",
"-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.003, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.032, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.01, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 2.3551, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.249, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.059, network: 0s)",
"-> NET_SSH2_MSG_CHANNEL_DATA (since last: 17.339, network: 0s)",
"<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.041, network: 0.041s)"
]
答案 0 :(得分:1)
AHHHH!在比喻地撞到墙上之后,我注意到每当我打电话$sftp->read()
时,它会稍微提高一点。所以我添加了一个do-while循环,当读取为空时停止。它奏效了!
$output->read = [];
$read = true;
do {
$read = $sftp->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX);
$output->read[] = $read;
} while ($read);
$sftp->read()
如果没有更多内容可以返回false。
我的完整代码如下:
$created = $sftp->write("matlab -nodisplay -nodesktop -nosplash -logfile remote_matlab.out -r \"cd('/some_supporting_files'); some_function('/some_data_I_just_uploaded.txt'); exit;\"\r\n");
$output->read = [];
$read = true;
do {
$read = $sftp->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX);
$output->read[] = $read;
} while ($read);
if(!$created){
//continue your script
}
我仍然无法使用$sftp->exec()
虽然上面的解决方案有效,但如果花费太长时间而没有给$ sftp-> read()读取内容,它就会停止工作。我仍然希望有人能以更强大的方式解决这个问题。