Linux, C++
I want to read a command as an argument to a program, and then run it using:
Code:
int returnValue = execlp( "/bin/sh", "/bin/sh", "-c", command, "; return $?", (char*)NULL );
The catch is, I want to know the PID of the program run using 'command'.
Using
Code:
ps ax | grep 'command'
may return more than one result, so it is not sufficient.
One proposed solution is to use
Code:
ps -o ppid,pid,command ax | grep 'command' | grep '^[ *]currentPID'
and then return the result in the second column. The problem with this is:
1) If the command is something very small and exits almost immediately
(Therefore not occurring in the ps results)
2) The command forks off additional children with similar command names
(Therefore returning more than one possibly correct result)