Sobes.tech
Junior — Middle

How can you check the process exit code in Linux?

sobes.tech AI

Answer from AI

In Linux, the process exit code can be checked using the environment variable $?, which contains the status of the last executed command.

For example:

./my_program
if [ $? -eq 0 ]; then
  echo "Program completed successfully"
else
  echo "Error executing program, code: $?"
fi

You can also obtain the process exit code from scripts or programs that launch processes and retrieve their status through system calls (for example, wait in bash or Process.ExitCode in C#). An exit code of 0 usually indicates successful completion, while any other value indicates an error.