Run system in background and print to command window when done
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi. In bash, I could run
{ echo "begin"; sleep 15; echo "finished" } &
This would run in the background, yet still output "finished" 15 seconds later. I find that this doesn't work the same when using system() in Matlab -- the final print doesn't output.
This is a simple example, but really I'm running a program from bash that takes a long time to complete and I'd like there to be an indication in my matlab command window when. it's done
0 comentarios
Respuestas (1)
Jan
el 18 de Mayo de 2021
I'm not sure if this works: You can let a timer check if a process called by java.exec has beenfinished already:
% [UNTESTED CODE!]
runtime = java.lang.Runtime.getRuntime();
proc = runtime.exec('YourExternalFunction');
TimerH = timer('TimerFcn', {@CheckProcessCB, 'check'}, ...
'StopFcn', {@CheckProcessCB, 'stop'}, ...
'StartDelay', 10, ...
'BusyMode', 'drop', ...
'ExecutionMode', 'fixedSpacing', ...
'Period', 10, ...
'UserData', proc);
start(TimerH);
pause(100);
stop(TimerH); % Kills the process also
function CheckProcessCB(TimerH, EventData, Cmd)
proc = TimerH.UserData;
switch Cmd
case 'check'
try
rc = proc.exitValue();
fprintf('External function has exited:\n');
dsip(rc)
catch % Process is still running
end
case 'stop'
fprintf(2, 'Stopping external process\n');
proc.destroy();
end
end
The pushing wastes some time, but calling this every 10 seconds should be fair
1 comentario
Serge
el 16 de Sept. de 2023
Editada: Serge
el 16 de Sept. de 2023
Here is a handy function that may help others:
function varargout = startcmd(cmd,vrb)
%Run a system command as a separate process, which can be monitored.
% startcmd(cmd) -start command
% startcmd(cmd,vrb) -verbose: 0=none, 1=show progress, 2=show results
% T = startcmd(__) -return timer for monitoring the process
%
%Remarks:
%-If output T is returned then it is up to the user to delete the timer
% when it is no longer required using T.delete
%-Use T.UserData to access process output when the process ends.
%-To stop the process prematurely use T.stop
%
%Example:
% startcmd('ping localhost',2) %start process with verbose=2
%
%Example:
% T = startcmd('ping localhost'); %start process and return the timer
% while T.Running=="on"
% pause(0.1) %wait for process to finish
% end
% T.UserData %process output
% T.delete %delete timer
if nargin<2 || isempty(vrb), vrb = 0; end
delTimer = ~nargout; %delete timer when the process ends
Proc = java.lang.Runtime.getRuntime.exec(cmd); %start process
Timer = timer(TimerFcn=@Update,StopFcn=@Stop,ExecutionMode='fixedSpacing',Period=0.1); %timer to monitor the process
Timer.start; %start timer
if nargout
varargout = {Timer}; %return timer, if requested
end
function Update(~,~)
if ~get(Proc,'Alive')
Timer.stop %stop the timer, which also calls the Stop function
end
end
function Stop(~,~)
Proc.destroy %close java runtime
t = java.util.Scanner(java.io.InputStreamReader(Proc.getInputStream)).useDelimiter('\A').next; %read process output
Timer.UserData = regexprep(strtrim(char(t)),'\r',''); %save output to Timer.UserData
if vrb >= 1, fprintf('Finished: %s\n',cmd), end %show progress
if vrb >= 2, fprintf('%s\n',Timer.UserData), end %show output
if delTimer, Timer.delete, end %delete timer
end
end
Ver también
Categorías
Más información sobre Platform and License en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!