2

How do i know, if a process has completed its execution without any errors? How do i know, if a C++ program has returned success to OS?

If i run it via shell, then i could use $?, however if i am checking the status of a process, initiated by other user, how could i check the status?

Say i started a process in morning, and it got terminated at noon. i have been workign on some other activities till evening, and prior leaving, i would like to check what the processes has returned to OS. how could i acheve that, programatically. Running through syslog would help, but looking for alternatives.

i could run through the OS's process table and read this information, however it sounds a bit complicated for my requirement. Do we have something like syslog, where all errors of processes are recorded?

Any other ways to retrieve errors reported by terminated processes (of other users too)?

kris123456
  • 501
  • 1
  • 5
  • 15
  • What about just wrapping the process in a shell script or C program that wrote the information out to the filesystem? You could then modify the calling program to execute the script rather than the process directly. That said, if you don't have those types of permissions that might be difficult. – beta Aug 26 '13 at 10:19
  • I cannot do that for all system processes. Also, i am trying to fetch the return values of processes, which were invoked by other users too. I cant insist all to wrap in a script. – kris123456 Aug 26 '13 at 10:34

1 Answers1

4

When a process terminates its parent process must acknowledge this using the wait or waitpid function. These functions also return the exit status. After the call to wait or waitpid the process table entry is removed, and the exit status is no longer stored anywhere in the operating system. You should check if the software you use to start the process saves the exit status somewhere.

If the parent process has not acknowledged that the child has terminated you can read its exit status from the /proc file system: it is the last field in /proc/[pid]/stat. It is stored in the same format that wait returns it, so you have to divide by 256 to get the exit code. Also you probably have to be root.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • IIRC you can read that file if you are the same uid as the process, or `root`. – nneonneo Aug 26 '13 at 12:05
  • You can read the file, but you need an extra permission to see the exit code. See the test that sets `permitted` in the `do_task_stat` function here: https://github.com/torvalds/linux/blob/master/fs/proc/array.c Do you know how to get that extra permission without becoming root? – Joni Aug 26 '13 at 12:07
  • I think having the same `uid/gid` automatically grants these permissions; see http://lxr.free-electrons.com/source/kernel/ptrace.c#L225 – nneonneo Aug 26 '13 at 12:22
  • I tested it, with my normal user account the exit code is printed as 0 (Ubuntu 13.04). Tracing further into the code it looks like it may depend on having `CAP_SYS_PTRACE` but I haven't confirmed if that's necessary or sufficient. – Joni Aug 26 '13 at 12:39