How Do You Know If a Program Ended Because It Completed Successfully, Had an Error, or Another User Interactively Terminated the Running Program?

Problem scenario
You believe that there are three main ways a program runs:  one it completed successfully, two an error made the program stop, or three another user on the server interactively terminated the program when it was still running.  You want to identify which one of these three possibilities happened.  How do you know if the program that just ran stopped because it completed successfully, had an error, or another user manually killed the running program?

Solution
Naturally Linux commands can do different things from what a human expects.  Some of these outcomes are considered "successes" insofar as the Linux OS is concerned.  To find which of the three outcomes happened, run this command: echo $?

The result will normally be a 0, 1 or 2.  To interpret this integer result, read the following:

  • A program that completes because it finished successfully will have an exit code of "0".
  • A program that ended with an error will have an exit code of "1".
  • An interrupted program will have an exit code of "2".  (The interrupt signal will stop a program correspond to the program's exit status being a 2.)

Leave a comment

Your email address will not be published.