how to terminate program in windows from Matlab?

Asked

Viewed 304 times

2

Is there any way to terminate a program in windows from a command in Matlab? Possibly using a batch... but I don’t know the commands to manage programs in windows or how to run it from Matlab.

I can have several instances of the same program, I want to finish all but the latest.

More details: during a cycle in Matlab a program in windows (Xfoil) is started thousands of times with different input parameters. Depending on the parameters, the program can block or enter an infinite cycle. After a certain time, the Matlab cycle leaves for the next iteration, opening a new instance of the same program, but the previous program continues to run, occupying the processor. Sometimes dozens accumulate completely blocking the computer.

  • You could show the part of the code where you start Xfoil. This would help.

  • I don’t know exactly because I use an intermediate Matlab program that exceeds my knowledge [link] (http://www.mathworks.com/matlabcentral/fileexchange/30478-xfoil-matlab-interface)

2 answers

0


I don’t know if there’s any more automatic way to solve this,
but I think that solves your problem.

First, you should find out what is the name of the process that is running in your code (I believe it is xfoil.exe).

So you should keep the code below running in parallel to your code or adapt it in a way that is executed between one execution and another of the xfoil in your program.

% Armazenará os IDs dos processos ativos.
pids_alive = containers.Map('KeyType','int32','ValueType','double');

% O tempo de verificação:
kill_time = 3; % 3 segundos

% O nome do processo que será encerrado.
nome_do_processo = 'xfoil.exe';

% O comando que encerrará o processo.
tasklist_cmd = sprintf('tasklist /fi "imagename eq %s" /nh /fo CSV > xfoils_running.csv', nome_do_processo);

% Loop ou Método que verificará a cada X secs o tempo
while 1 

    ids = keys(pids_alive);
    tempos = values(pids_alive);

    % Mata todos os processos que estavam rodando na passo anterior:
    for i = 1:length(ids)
        if (cputime - cell2mat(tempos(i))) > (0.1 * kill_time)
            % Comando que mata o processo.
            cmdkiller = sprintf('taskkill /f /pid %d', cell2mat(ids(i)));
            system(cmdkiller);

            % Remove o ID da lista.
            remove(pids_alive, cell2mat(ids(i)));
        end
    end

    % Lista os processos atualmente rodando.
    % Salva a lista dos processos encontrados no arquivo
    % xfoils_running.csv.
    system(tasklist_cmd)

    % Lê a lista criada.
    fid = fopen('xfoils_running.csv', 'r');

    while not(feof(fid))
        % Lê uma linha do arquivo.
        tline = fgetl(fid);

        % Lê uma linha da lista de processos.
        res = textscan(tline,'%q%q%q%q%q','delimiter',',');

        % Se não encontrar nenhum processo vivo.
        if isempty(res{2})
            continue;
        end

        % Lê ID do processo.
        pid = str2num(cell2mat(res{2}));

        % Armazena o contador tempo de vida do processo
        % se ainda não estava sendo monitorado.
        if isempty(find(cell2mat(ids) == pid))
            pids_alive(pid) = cputime;
        end
    end

    % Encerra o arquivo anterior.
    fclose(fid);

    % Espera 1 segundo
    pause(1)
end

What solves, basically, the problem is the commands tasklist, used to list active processes and get the ID of prossos:

tasklist /fi "imagename eq xfoil.exe" /nh /fo CSV > xfoils_running.csv

From there, just monitor the time that these processes live. If the time is longer than what you believe is the limit, you kill the process by using ID taskkill:

taskkill /f /pid ####

0

"tskill "nomedoprocess" without quotation marks

  • 2

    Hi, Welcome to [en.so], can you explain your solution better? Check out the [Sweswer] guide. You can [Dit] the answer at any time.

  • I have several processes with the same name, I want to finish all but the latest, has way to do this?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.