Linux系统按名称查找程序并杀死程序

发布时间 2023-11-06 17:14:28作者: 一字千金

linux还是window系统,打开程序前先要关闭之前打开程序和子程序,可以按照名称来杀死进程;多个进程也可以用一个命令全部杀死;

    QProcess p;
#ifdef _WIN32
    QString videoplayer = "taskkill /f /im videoplayer.exe";    //exeFileName为要杀死的进程名
    QString AnalysisCenter = "taskkill /f /im AnalysisCenter.exe";    //exeFileName为要杀死的进程名
    QString regionfilter= "taskkill /f /im regionfilter.exe";    //exeFileName为要杀死的进程名
    QString qstrExeName = "OvitFrame.exe";
#else
    QString videoplayer = "killall -9 videoPlayer";    //exeFileName为要杀死的进程名
    QString AnalysisCenter = "killall -9 AnalysisCenter";    //exeFileName为要杀死的进程名
    QString regionfilter = "killall -9 regionfilter";    //exeFileName为要杀死的进程名
    QString qstrExeName = "OvitFrame";
#endif

    p.execute(videoplayer);
    p.execute(AnalysisCenter);
    p.execute(regionfilter);
    p.close();

如果想根据名称查找到进程,再根据进程Id杀死其中部分程序,则可以按照下面的方式来实现。

void OvitApplication::KillAnotherProcess(const QString &appName, const quint64 &quintId)
{
    LOG_INFO("KillAnotherProcess %s", appName.toStdString().c_str());
    bool res = false;
#ifdef _WIN32
    MODULEENTRY32 me32 = { 0 };
    HANDLE    hToolHelp32Snapshot;
    hToolHelp32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    PROCESSENTRY32    pe = { sizeof(PROCESSENTRY32) };
    BOOL  isSuccess = Process32First(hToolHelp32Snapshot, &pe);
    while (isSuccess)
    {
        char cExePath[260] = { 0 };
        char cExeAbsPath[260] = { 0 };
        sprintf(cExePath, "%S", pe.szExeFile);
        string strExePath = cExePath;
        if (0 == strExePath.compare(appName.toStdString().c_str()))
        {
            HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe.th32ProcessID);
            if (pe.th32ProcessID == quintId)
            {
                isSuccess = Process32Next(hToolHelp32Snapshot, &pe);
                continue;
            }
            if (hProcess)
            {
                if (GetModuleFileNameExA(hProcess, NULL, cExeAbsPath, MAX_PATH) == 0)
                {
                    LOG_INFO("GetModuleFileNameExA error=%d cExeAbsPath:%s", GetLastError(), cExeAbsPath);
                }
                else
                {
                    string strExeAbsPath = cExeAbsPath;
                    QProcess p;
                    QString c = "taskkill /pid " + QString("%1").arg(pe.th32ProcessID) + " /f";
                    LOG_INFO("KillAbsPathProcess find Process Running,cmd:%s", c.toStdString().c_str());
                    p.execute(c);
                    p.close();
                    CloseHandle(hProcess);
                }
            }
            //CloseHandle(hProcess);
        }
        isSuccess = Process32Next(hToolHelp32Snapshot, &pe);
    }
    CloseHandle(hToolHelp32Snapshot);
#else
    QProcess process;
    process.start("pidof OvitFrame");
    if (process.waitForFinished())
    {
        QString bytes = process.readAll();
        if (bytes.contains(QString::number(quintId)))
        {
            bytes.remove(QString::number(quintId));
            if (bytes!=""&& bytes!=" ")
            {
                process.execute("kill -9 " + bytes);
            }
        }
    }
#endif
}

 

//exeFileName为要杀死的进程名