Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to kill process in c++, knowing only part of its name
    primarykey
    data
    text
    <p>Some time ago I needed to write c++ code to kill some process. In my main program I run large CAE-system package with system("...") with different filename strings on input. CAE-software creates many processes, that contain in process name string <em>filename</em>). Some of the CAE-processes <em>worktime</em> > <em>max_time</em>, than I need to shut them down:</p> <pre><code>//filename contains part of CAE-process name string s="/bin/kill -9 `ps aux | grep "+filename+" | awk {'print $2'}`"; system(s.c_str()); </code></pre> <p>The output was:</p> <pre><code>Usage: kill pid ... Send SIGTERM to every process listed. kill signal pid ... Send a signal to every process listed. kill -s signal pid ... Send a signal to every process listed. kill -l List all signal names. kill -L List all signal names in a nice table. kill -l signal Convert between signal numbers and names. </code></pre> <p>I tried to run with execvp, tried <a href="https://stackoverflow.com/a/9402442/1566267">different ways running kill or pkill over bash script</a>, calling system("name_of_script.sh"), where script contained <code>kill -9 *filename*</code> but the result was the same.<br> Using <code>kill</code> and <code>/bin/kill</code> gave the same output, <code>bash -c kill...</code> too.<br> Using <strong><code>kill</code> from my system</strong> (Ubuntu Natty) <strong>gnome-terminal</strong>: </p> <pre><code>kill -9 `ps aux | grep filename | awk {'print $2'}` </code></pre> <p>shutdown all necessary processes! It <strong>works</strong>.</p> <p>When using <code>pkill</code>, as I could understand, we need full process name to kill it, but I have only part of name.</p> <p>I also tried to wrap computational process into a child thread using pthreads and stop it with pthread_cancel, but it doesn't work because of CAE-system process doesn't receive signals (I think, trapping them), the only way is SIGTERM.<br> Killing child-thread-"wrap" with pthread_kill also kills the parent (my main program).</p> <p>I don't know CAE-process pids to call <a href="http://linux.die.net/man/2/kill" rel="nofollow noreferrer">kill from signals.h</a><br> Closing main program do not stop CAE-processes (and the do not have -Z flag, so are they aren't my program process childs??)</p> <p>How can I close CAE-processes, that run > MAXTIME from my main program?</p> <hr> <p><strong><em>The problem was that I was running main program via debugger (gdb) in QtCreator. Without QtCreator shell-script runs with arguments the right way, though arguments are passed correctly both ways.</em></strong></p> <hr> <p>Also I have to clear some CAE processes, that don't have filename in cmdline, but that are parents or children of this process. In shell-script you can use:</p> <pre><code>cat /proc/"$P"/status | grep PPid | grep -o "[0-9]*" </code></pre> <p>where <code>$P</code> is a variable with pid of killed process. <a href="https://stackoverflow.com/questions/7138783/how-to-display-the-current-process-tree-of-a-bash-session">Here are several methods</a> to kill all child processes.</p> <p>I'll write smth. similar in C++ that will scan <code>/proc/xxxx/status</code> till <code>PPid</code>= ppid_of_my main_program and cut that branch.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload