Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

c - Check exit code of an unrelated process

I want to check the exit code of a foreground process using C code running on linux. As I understand, wait() and waitpid() are useful for child processes, but in my case, it is a foreign process. I am able to read information from /proc/<pid>/stat for that process while it is active, but as the process closes, reading from /proc/<pid>/ becomes problematic and I didn't find any information relating to exit code.

Other things I've tried:

popen() some bash commands. echo $? always returned 0, even when process of interest exited with an error code. I am not sure it targeted the process of interest. Another bash command I tried to call, was wait <pid> but this command returned immediately, while the process was still running.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have access to the foreground process' code, you can send a message via a message queue or even a socket (e.g. udp multicast) - and that will make the solution more general (your c program can run on a different machine).

Another option is to use a loggging service (syslog or something like that). it has some useful interfaces that enable processes to log their exit codes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...