2014-09-01 53 views
1

正在運行的程序Matlab的我有一個調用特定的MATLAB程序或Perl腳本中使用命令,如在特定的時間運行腳本:殺死現有/ Perl的

MATLAB

system('matlab', '-nosplash', '-nodisplay', '-nodesktop', '-wait', '-logfile', "$workDir/$job_name.log", '-r', "$job_args;exit"); 

PERL

system("perl $jobDir/$job_args > $workDir/$job_name.out 2> $workDir/$job_name.err"); 

然後我有一些代碼封裝了上面的命令(每個都運行在不同的腳本中),如果運行超過2小時或運行失敗/出現錯誤,則使用警報來終止腳本小人。

問題是殺死正在運行的matlab進程似乎非常困難。如果一個m文件遇到錯誤,它只會掛在那裏,永遠不會完成或傳遞到'退出'。 2小時的限制仍然達到,但它只是繼續。死似乎並沒有殺死實際的matlab腳本。

從Perl腳本中,有一種方法可以在2小時後終止正在運行的matlab腳本,或者在執行過程中發生錯誤嗎?

周圍的腳本代碼的一個例子是這樣的:

eval{ 
     local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required 
     alarm 7200; # 2 hour timeout 
     my $test = system('matlab', '-nosplash', '-nodisplay', '-nodesktop', '-wait', '-logfile', "$workDir/$job_name.log", '-r', "$job_args;exit"); 
     #Run matlab with nosplash screen or matlab instance. -wait forces the script to pause until the matlab program completes. Logfile is writte to NAME.log, after prog completes, matlab closes. 
     if ($test != 0) { 
     die "Failed to run"; 
     print "$job_name failed: $?>>8"; 
     exec("perl", "$scriptDir/msched_result.pl", "$job_name", "FAILED", "$start_time");  #print argsfile output to .out and error to .err. 
     } 
     alarm 0; 
     }; 
     if ([email protected]) { 
      die unless [email protected] eq "alarm\n"; # Propagate unexpected errors 
      exec("perl", "$scriptDir/msched_result.pl", "$job_name", "TIMED OUT", "$start_time") 
     } else { 
      exec("perl", "$scriptDir/msched_result.pl", "$job_name", "DONE", "$start_time"); #run msched_result 
     } 

感謝

+0

我現在還沒有安裝matlab,但是如果我記得好,'matlab'命令是一個很大的bash腳本,叫做matlab-binary。你可以檢查在shell腳本中做了什麼,(也許它做了一些特殊的信號處理等)或者,你可以檢查[Math :: Matlab :: Local]的源代碼(https:// metacpan。 org/pod/Math :: Matlab :: Local)的一些想法... – jm666 2014-09-01 10:28:01

+0

http://stackoverflow.com/questions/2679582/how-can-i-kill-a-perl-system-call-after-a -timeout?rq = 1 – 2014-09-01 10:31:42

+0

查找'IPC :: Open2' - 允許您打開進程的exec管道,並保留pid(必要時用於查殺) – Sobrique 2014-09-01 14:22:02

回答

0

如果任何人有類似的問題對我來說,殺了MATLAB文件有錯誤的解決方案是預先 - 通過以某種方式調用文件來預期它。

而不是我的第一個例子:

system('matlab', '-nosplash', '-nodisplay', '-nodesktop', '-wait', '-logfile', "$workDir/$job_name.log", '-r', "$job_args;exit"); 

嘗試

system('matlab', '-nosplash', '-nodisplay', '-nodesktop', '-wait', '-logfile', "$workDir/$job_name.log", '-r', "try,$job_args,catch,exit(1),end,exit(0)"); 

所以不管是否有錯誤或不運行參數時,MATLAB將關閉,取決於1或0返回是否發生錯誤。

或者

`taskkill /F /IM matlab.exe 2>&1`; 

會殺了MATLAB的所有實例,但是這是非常強力。