- #1
ORF
- 170
- 18
hello
I needed to count the number of processes (with the same name). I found that this bash script works,
and the exit value can be caught by this code
This way is dirty and limited to linux.
Can this job be done by a C/C++ routine, non-OS dependent? Or at least, can this job avoid calling "ps cax | grep firefox | wc -l" as an external bash script?
Thank you for your time.
Regards,
ORF
I needed to count the number of processes (with the same name). I found that this bash script works,
Code:
#!/bin/bash
exit $(ps cax | grep firefox | wc -l);
and the exit value can be caught by this code
Code:
#include <stdlib.h> // sytem
#include <iostream> // std::cout, std::endl
int main()
{
int ret = system("./myBashScript.sh 2>&1 > /dev/null");
std::cerr << "Number of firefox sessions: " << WEXITSTATUS(ret) << std::endl;
return 0;
}
This way is dirty and limited to linux.
Can this job be done by a C/C++ routine, non-OS dependent? Or at least, can this job avoid calling "ps cax | grep firefox | wc -l" as an external bash script?
Thank you for your time.
Regards,
ORF