Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good Linux exit error code strategy?

I have several independent executable Perl, PHP CLI scripts and C++ programs for which I need to develop an exit error code strategy. These programs are called by other programs using a wrapper class I created to use exec() in PHP. So, I will be able to get an error code back. Based on that error code, the calling script will need to do something.

I have done a little bit of research and it seems like anything in the 1-254 (or maybe just 1-127) range could be fair game to user-defined error codes.

I was just wondering how other people have approached error handling in this situation.

like image 446
Chris Kloberdanz Avatar asked Dec 26 '08 14:12

Chris Kloberdanz


People also ask

What is a successful exit code?

# By convention, an 'exit 0' indicates success, #+ while a non-zero exit value means an error or anomalous condition.

How do you exit a code in Linux?

To set an exit code in a script use exit 0 where 0 is the number you want to return. In the following example a shell script exits with a 1 . This file is saved as exit.sh . Executing this script shows that the exit code is correctly set.

What is the exit code for a successful command execution Linux?

In Linux, when a process is terminated, it returns an exit code. Upon successful execution, this code is equal to zero. Any non-zero exit code indicates that some error occurred.

What are valid exit statuses?

The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred to as an errorlevel.


1 Answers

The only convention is that you return 0 for success, and something other than zero for an error. Most well-known unix programs document the various return codes that they can return, and so should you. It doesn't make a lot of sense to try to make a common list for all possible error codes that any arbitrary program could return, or else you end up with tens of thousands of them like some other OS's, and even then, it doesn't always cover the specific type of error you want to return.

So just be consistent, and be sure to document whatever scheme you decide to use.

like image 167
Nik Reiman Avatar answered Oct 10 '22 08:10

Nik Reiman