When editing a Linux PHP shell script today, I got the error:
EXTENSION 'script.php' NOT PRESENT
The problem was that the file was saved with Windows line endings (\r\n
). I just had to re-save it with UNIX line endings and it worked fine.
When editing a Linux PHP shell script today, I got the error:
EXTENSION 'script.php' NOT PRESENT
The problem was that the file was saved with Windows line endings (\r\n
). I just had to re-save it with UNIX line endings and it worked fine.
I have a PHP script that I will be running in one of three ways:
In order to do this I found that I can do the following:
#!/usr/bin/php -q <?php @ob_end_clean(); if (isset($argv)) { define("SHELL",true); $options = getopt("c"); if (isset($options['c'])) { echo "Running as a cron job ...\n"; define("INTERACTIVE",false); } else { echo "Running interactively ...\n"; define("INTERACTIVE",true); } } else { echo "Running via Apache... "; define("INTERACTIVE",false); define("SHELL",false); }
The general idea is:
ob_end_clean
isn’t available when running from the CLI.$argv
array to determine if we are running via CLI, it’s not set when run through mod_phpgetopt
to read the parameters and isset
to check if the “-c” parameter was passed.SHELL
and INTERACTIVE
constants can be used to determine if we are in a shell or Apache, and interactive or cron-mode, respectively.Finally, don’t forget to chmod the script to make it executable.
chmod 700 script.php