PHP shutdown handlers and exit codes

I was in bed trying to get to sleep but my brain wanted to know the answer to this question. So I was forced out of bed to write this experiment:

function main( $argv ) {

  register_shutdown_function( 'shutdown_1' );
  register_shutdown_function( 'shutdown_2' );

  exit( 0 );

}

function shutdown_1() {

  exit( 1 );

}

function shutdown_2() {

  exit( 2 );

}

main( $argv );

With this PHP code, what do you expect is the resultant error level?

The answer is ‘1’. After main() calls exit( 0 ) the shutdown function shutdown_1() is invoked. When shutdown_1() calls exit( 1 ) the process exists and shutdown_2() is never called.

I’m glad we cleared that up. Back to bed.

PHP finally blocks not run on exit

I confirmed with the following code that if you call ‘exit’ withing a ‘try’ block the ‘finally’ block does *not* execute. That’s probably what you would expect. But now we know.

register_shutdown_function( 'handle_shutdown' );

try {

  exit;

}
catch ( Exception $ex ) {

  echo "caught...\n";

}
finally {

  echo "finally...\n";

}

function handle_shutdown() {

  echo "shutdown...\n";

}

Error: post-commit hook failed (exit code 255) with no output

In subversion I was getting the error “post-commit hook failed (exit code 255) with no output” after trying to configure my post-commit hook to send email notifications. At first I thought the problem must have been related to the mailer, but I ran the mail command manually and it worked fine. Eventually I figured out that the problem was that the hooks/post-commit file hadn’t been marked executable. So it was a simple chmod +x to fix the problem.

Bash wait

Today I learned about the ‘wait’ command. It waits for background processes to terminate before returning, so you can fire off a bunch of jobs to be run in parallel and then wait for all of them to complete before continuing, like in this take-ownership.sh script I wrote tonight:

#!/bin/bash
if [ -n "$1" ]; then
  pushd "$1" > /dev/null 2>&1
  if [ "$?" -ne "0" ]; then
    echo "Cannot change dir to '$1'.";
    exit 1;
  fi
fi
sudo chown -R jj5:jj5 . &
sudo find . -type d -exec chmod u+rwx {} \; &
sudo find . -type f -exec chmod u+rw {} \; &
if [ -n "$1" ]; then
  popd > /dev/null 2>&1
fi
wait
exit 0