Run system in background and print to command window when done

27 views (last 30 days)
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

Answers (1)

Jan
Jan on 18 May 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 Comment
Serge
Serge on 16 Sep 2023
Edited: Serge on 16 Sep 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

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!