NodeJS子进程命令以检查正在运行的进程

问题描述

让我的命令按需运行有些困难。我不能告诉我是否需要更改代码或命令。

目标: ProgramA检查ProgramB是否正在运行。 Grep返回匹配的进程。标志返回true。 A连接到B。如果未运行,则Flag返回False。

我认为问题出在我的子进程命令中,用于查询活动进程。它检查进程数并返回计数。如果程序正在运行,则命令返回数字(例如4),而trueFalse返回true。如果ProgramB未运行,则命令返回0。trueFalse标志返回false。

深入研究之后:似乎我在trueFalse中的逻辑不好吗?我试图获取标准输出,并将其放入字符串,然后检查其是否大于数字。似乎总是返回false。但我不知道为什么。它使我发疯。我肯定它也很简单。

代码

// Variable for application process we are checking.
let query = 'ProgramB';

const programCheck = (query,trueFalse) => {
        // Using nodeJS to find the OS
        let platform = process.platform;
        // Variable to hold command to send to terminal
        let cmd = ''; 

        // Switch for the platform to distinguish which OS the user has - So we can use the
        // correct command for querying the active processes. 
        switch(platform) {
            // Windows 
            case 'win32' : cmd = `tasklist /FI "IMAGENAME eq ${query}.exe"`;
            break;
            // Mac
            case 'darwin' : cmd = `ps -ax | grep -v grep | grep ${query}`;
            break;
            // Linux
            case 'linux' : cmd = `ps -ef | grep '${query}'`; 
            break; 
            // Default - Other
            default : console.log(`UnkNown Operating System,unable to check ${query}`);
            break;
        }

        // Calling child process
        exec(cmd,(err,stdout,stderr) => {
            trueFalse(stdout.toLowerCase().indexOf(query.toLowerCase()) > -1);
            if (err) throw err;
        });
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)