Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exit(1) in file results in script status code 0

Tags:

php

On Ubuntu machine:

$ php -v
PHP 5.5.10-1~dotdeb.1 (cli) (built: Mar  6 2014 18:55:59) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with uopz v2.0.4, Copyright (c) 2014, by Joe Watkins <[email protected]>
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

My test.php file is simple:

<?php 
exit(1); 

I would expect this command php test.php || echo "error" to show "error" but it exits with status code 0.

$ php test.php
$ echo $?
0

But on the same machine the same code, but not in file works as expected:

$ php -r "exit(1);" || echo "error"
error

or

$ php -r "exit(1);"
$ echo $?
1

On different machine (archlinux) with php:

PHP 5.5.13 (cli) (built: May 29 2014 05:46:58) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

All cases work as expected, even when the code is run from file the status code is 1.

This is a true problem because git hooks depends on this status codes and Jenkins and I couldn't google it out.

Could it be somehow config-related? I checked cli php.ini and couldn't find anything suspicious.

like image 728
Grzegorz Avatar asked Jun 25 '14 09:06

Grzegorz


People also ask

What does exit 0 mean in shell script?

Linux / Unix Community You normally use exit(0) if everything went ok. But if your program detects an error and decides to abort, you would use exit(1). Or you might use exit(1), exit(2), etc, with each exit code meaning some specific error.

What is exit 1 in shell script?

Exit codes are a number between 0 and 255, which is returned by any Unix command when it returns control to its parent process. Other numbers can be used, but these are treated modulo 256, so exit -10 is equivalent to exit 246 , and exit 257 is equivalent to exit 1 .

What does an exit status of 0 mean?

Shell scripts typically execute commands and capture their exit statuses. For the shell's purposes, a command which exits with a zero exit status has succeeded. A nonzero exit status indicates failure.

What is the difference between exit 0 and exit 1?

Both the functions, exit(0) and exit(1), are used to exit out of the program, but there is one major difference between exit(0) and exit(1). The exit (0) shows the successful termination of the program and the exit(1) shows the abnormal termination of the program.


1 Answers

The uopz extension is the problem. It "corrupts" the exit code. There was a bug opened about this issue.

You can try to set the configuration uopz.overloads=0 as it was recommended in the bug comments. That, unfortunately didn't work for me. Only disabling the extension fixed the issue.

like image 158
Christian P Avatar answered Oct 23 '22 08:10

Christian P