How to kill process in c++, knowing only part of its name

前端 未结 2 879
遇见更好的自我
遇见更好的自我 2021-01-06 10:59

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 inpu

2条回答
  •  灰色年华
    2021-01-06 11:38

    This should just work assuming filename isn't too much exotic or contains a regular expression pattern:

    string s="pkill -9 -f "+filename";
    system(s.c_str());
    

    As a side note, -9 is a last resort signal, not something you should start with. I would thus recommend the less brutal:

    string s="pkill -f "+filename"+";sleep 2; pkill -9 -f "+filename;
    system(s.c_str());
    

提交回复
热议问题